replacing or adding dimstyles from another drawing

Discussion in 'AutoCAD' started by Tom Hanley, Mar 10, 2005.

  1. Tom Hanley

    Tom Hanley Guest

    I have to update dimstyles in the current drawing (about 6) with the
    definitions from another drawing. Anyone have a routine for that?
    Thanks,
    Tom
     
    Tom Hanley, Mar 10, 2005
    #1
  2. Tom Hanley

    Bill DeShawn Guest

    No routine necessary. Delete the old DIMENSION STYLES and insert the other
    drawing. No need to give the drawing an insertion point. You can ESCape.
    But then you will have your new Dimension Styles.
     
    Bill DeShawn, Mar 11, 2005
    #2
  3. Tom Hanley

    Tom Hanley Guest

    That won't work for drawings where the dimstyles are being used. I want to
    modify the existing dimstyles and add ones that aren't there. I had used
    scripts in the past for this but I wanted a better solution than that.
     
    Tom Hanley, Mar 11, 2005
    #3
  4. Tom Hanley

    OLD-CADaver Guest

    CHECKSTANDARDS?
     
    OLD-CADaver, Mar 11, 2005
    #4
  5. Tom Hanley

    Tom Hanley Guest

    I was playing with that but it seems you would have to attach a check stds
    file to each drawing then run check standards then fix each prooblem
    separately. Not as simple as a replace existing dimstyle with one from
    another drawing.
     
    Tom Hanley, Mar 11, 2005
    #5
  6. Tom Hanley

    Tom Hanley Guest

    I tried the express tools import export dimstyles tool as well but it
    doesn't seem to work as I expected. When I tell it to replace existing
    dimstyle, it created a new cryptic dimstyle name and never changed the
    existing dimstyle. I'll post in express tools for clarification.
     
    Tom Hanley, Mar 11, 2005
    #6
  7. Tom Hanley

    Joe Burke Guest

    Tom,

    I have some rough code which might help.

    Need to know two things. What version are you running? Also, are the dimstyle names
    to be updated the same in the current/active drawing and the source drawing?

    Joe Burke
     
    Joe Burke, Mar 12, 2005
    #7
  8. Tom Hanley

    Tom Hanley Guest

    I am using LDT2005. My intention is to add missing dimstyles and overwrite
    any dimstyles with duplicate names in the active with the definitions in
    some standard drawing (could be dwg, dws or dwt, whatever works). I am able
    to do this using expresstools import dimstyles but I can't seem to do it
    silently. It forces a dialog box interface. Also, this method required me to
    edit the .dim file to overwrite the existing dimstyles).
     
    Tom Hanley, Mar 14, 2005
    #8
  9. Tom Hanley

    Joe Burke Guest

    Tom,

    Understood. I'll post some code once I have it working. I think I'm close.

    Joe Burke
     
    Joe Burke, Mar 14, 2005
    #9
  10. Tom Hanley

    Joe Burke Guest

    Tom,

    I've been fooling with this, as time permits, since my last post.

    The add missing dimstyles part is easy. Of course you could do that with Design
    Center.

    The update (or now that I reread your last post, "overwrite") existing dimstyles
    turned out to be more difficult than I expected. What I was trying to do was update
    the dimstyle settings in target document with the settings from source document given
    duplicate dimstyles names in both files. Then I assumed any existing dimensions in
    target doc would need to be updated with the modified style settings.

    I was partly successful doing that, but the operation often left ACAD in an unstable
    state. IOW, ready to crash with a fatal error. I don't know why.

    Are you saying existing dimensions in target doc are of no concern? They could be
    erased, which would allow their dimstyles to be deleted. Or maybe there are no
    dimensions in target doc? If either one of those is the case, then I think I can do
    it.

    Joe Burke
     
    Joe Burke, Mar 18, 2005
    #10
  11. Tom Hanley

    Tom Hanley Guest

    Joe,
    Thanks for taking the time to figure this out. The target dwg would have
    dimensions that would require updating as well. Don't go nuts with this... I
    got a semi automatic solution where I am using the express tools import
    dimstyles using a modified definition file which will overwrite and bring in
    new dimstyles. It seems to run fine it's just not an automatic solution that
    can be lisp or icon button run...
     
    Tom Hanley, Mar 18, 2005
    #11
  12. Tom Hanley

    OLD-CADaver Guest

    You could try going the other way. Create a file with all the new dimstyles properly set, then import the existing drawing into it, then saveas.
     
    OLD-CADaver, Mar 18, 2005
    #12
  13. Tom Hanley

    Tom Hanley Guest

    That would be a little clumsy with lots of paperspace layouts...
     
    Tom Hanley, Mar 18, 2005
    #13
  14. Tom Hanley

    OLD-CADaver Guest

    <<That would be a little clumsy with lots of paperspace layouts>>

    Yes it would be with annotation in PS and different styles spread across multiple tabs. But from other threads on these boards, it seems that those of us in that catagory are in a minority.

    But, If all the styles reside in MS, that may be a reasonable option to consider.
     
    OLD-CADaver, Mar 18, 2005
    #14
  15. Tom Hanley

    Joe Burke Guest

    Tom,

    I'm thinking about a different approach. Check back Monday.

    Joe Burke
     
    Joe Burke, Mar 19, 2005
    #15
  16. Tom Hanley

    Joe Burke Guest

    Tom,

    I think this does the trick.

    Test with copies of working files first. I might not have considered every possible
    condition. Undo is available if things go wrong. Error checking is fairly complete,
    but I wouldn't call it bulletproof.

    Code:
    ;; Joe Burke  3/19/2005
    ;; Import dimstyles which don't exist in the active file.
    ;; Update dimstyles in the active file which exist in both files.
    ;; Note: existing associated dimensions will be disassociated.
    (defun c:ImportDimStyles (/ *error* GetDoc doc targstyles oldcnt fullpath
    srcdoc srcstyles actdimstyle name exstnamelst
    srcnamelst copylst endnamelst duplst
    tempstyle namelst newcnt)
    
    ;;local functions
    ;;-------------------------------------------------
    (defun *error* (error)
    (cond
    ((not error))
    ((wcmatch (strcase error) "*QUIT*,*CANCEL*"))
    (T (princ (strcat "\nERROR: " error)))
    )
    (if
    (and
    srcdoc
    (not (vlax-object-released-p srcdoc))
    )
    (vlax-release-object srcdoc)
    )
    (vla-EndUndoMark doc)
    (princ)
    ) ;end error
    
    ;Returns either a doc from the documents collection
    ;or an ObjectDBX doc.
    ;Note, versions earlier than 2004 need modified code
    ;if axdb15.dll is not registered.
    (defun GetDoc ( path / *acad* documents srcdoc )
    (setq *acad* (vlax-get-acad-object)
    documents (vla-get-documents *acad*)
    )
    ;check the documents collection
    (vlax-for x documents
    (if (= path (vlax-get x 'FullName))
    (setq srcdoc x)
    )
    )
    ;or use ObjectDBX
    (if (null srcdoc)
    (progn
    (setq srcdoc
    (vla-GetInterfaceObject *acad* "ObjectDBX.AxDbDocument.16")
    )
    (vla-open srcdoc path)
    )
    )
    srcdoc
    ) ;end
    ;;-------------------------------------------------
    
    (setq doc (vla-get-activedocument (vlax-get-acad-object)))
    (vla-EndUndoMark doc)
    (vla-StartUndoMark doc)
    
    (setq targstyles (vla-get-dimstyles doc)
    oldcnt (vlax-get targstyles 'Count)
    fullpath (getfiled "Select Source File" (getvar "dwgprefix") "dwg" 0)
    )
    
    (if (not fullpath)
    (progn
    (princ "\nSource document not selected. Exiting... ")
    (exit)
    )
    )
    
    (setq srcdoc (GetDoc fullpath)
    srcstyles (vla-get-dimstyles srcdoc)
    actdimstyle (vlax-get doc 'ActiveDimStyle)
    )
    
    ;list the dimstyle names in each document and determine
    ;what the name list in target doc should be after import
    (vlax-for x targstyles
    (setq exstnamelst (cons (vlax-get x 'Name) exstnamelst))
    )
    (vlax-for x srcstyles
    (setq copylst (cons x copylst))
    (setq name (vlax-get x 'Name))
    (setq srcnamelst (cons name srcnamelst))
    (if (not (vl-position (strcase name) (mapcar 'strcase exstnamelst)))
    (setq endnamelst (cons name endnamelst))
    (setq duplst (cons name duplst))
    )
    )
    (setq endnamelst (append exstnamelst endnamelst))
    
    ;rename dimstyles to be updated
    (foreach x duplst
    (vlax-put (vla-item targstyles x) 'Name (strcat "%%" x))
    )
    
    ;copy all source doc dimstyles to target doc
    (vlax-invoke srcdoc 'CopyObjects copylst targstyles)
    
    (vlax-release-object srcstyles)
    (vlax-release-object srcdoc)
    
    (if duplst
    (progn  ;------------
    
    ;add a temporary dimstyle and make it the active dimstyle
    (setq tempstyle (vlax-invoke targstyles 'Add "tempdimensionstyle"))
    (vlax-put doc 'ActiveDimStyle tempstyle)
    
    ;copy dimstyle settings from source style to target style
    (foreach x duplst
    (vla-CopyFrom
    (vla-item targstyles (strcat "%%" x))
    (vla-item targstyles x)
    )
    )
    
    ;first delete temp child styles
    (vlax-for x targstyles
    (setq name (vlax-get x 'Name))
    (if
    (and
    (vl-position name duplst)
    (vl-string-search "$" name)
    )
    (vl-catch-all-apply 'vla-delete (list x))
    )
    )
    
    ;then delete temp parent styles
    (vlax-for x targstyles
    (setq name (vlax-get x 'Name))
    (if (vl-position name duplst)
    (vl-catch-all-apply 'vla-delete (list x))
    )
    )
    
    ;rename styles to original names
    (vlax-for x targstyles
    (setq name (vlax-get x 'Name))
    (if (vl-string-search "%" name)
    (vlax-put x 'Name (vl-string-left-trim "%" name))
    )
    )
    
    ;update existing dimensions with new style settings
    (if (ssget "X" (list (cons 0 "DIMENSION") (cons 410 (getvar "ctab"))))
    (vlax-for x (vlax-get doc 'ActiveSelectionSet)
    (vla-update x)
    )
    )
    
    ;restore the active dimstyle
    (vlax-put doc 'ActiveDimStyle actdimstyle)
    ;delete the temporary dimstyle
    (vl-catch-all-apply 'vla-delete (list tempstyle))
    
    )  ;------------
    ) ;if
    
    ;Reports:
    ;get the updated list of dimstyle names and compare
    ;with anticipated list: endnamelst
    (vlax-for x targstyles
    (setq namelst (cons (vlax-get x 'Name) namelst))
    )
    
    ;option
    ;(textscr)
    
    (if (equal (acad_strlsort namelst) (acad_strlsort endnamelst))
    (princ "\nImport operation was successful. ")
    (princ "\nImport encountered a problem. Undo or check dimstyles. ")
    )
    
    (if (< oldcnt (setq newcnt (vlax-get targstyles 'Count)))
    (princ (strcat "\nNumber of dimstyles added: " (itoa (- newcnt oldcnt))))
    )
    
    (if duplst
    (princ (strcat " Number of dimstyles updated: " (itoa (length duplst))))
    )
    
    (*error* nil)
    ) ;end
    
    ;; -----------------------------
    ;; shortcut
    (defun c:IDS () (c:ImportDimStyles))
    ;; -----------------------------
    
     
    Joe Burke, Mar 20, 2005
    #16
  17. Tom Hanley

    Dave Jones Guest

    Joe,
    that's really slick! Could there be a way to get dimstyles from a .dwt file
    also?
    Dave
    DDP
     
    Dave Jones, Mar 20, 2005
    #17
  18. Tom Hanley

    Joe Burke Guest

    Dave,

    Yes. Change line 64 as follows.

    from this
    fullpath (getfiled "Select Source File" (getvar "dwgprefix") "dwg" 0)
    to this
    fullpath (getfiled "Select Source File" (getvar "dwgprefix") "dwg;dwt" 0)

    Thanks for the suggestion.

    Joe Burke
     
    Joe Burke, Mar 21, 2005
    #18
  19. Tom Hanley

    Tom Hanley Guest

    Thanks Joe!
    It seems to work fine. I will hard code the path of the template file to
    make it silent. Thanks for all your time and effort. I realized early on
    that this kind of coding was way beyond my level.
    Tom
     
    Tom Hanley, Mar 21, 2005
    #19
  20. Tom Hanley

    Joe Burke Guest

    Tom and Dave,

    Revised version enclosed.

    Fixes a bug and tweaks a few things. Source file can be DWG, DWT or DWS. Existing
    dimensions/leaders in model space and layouts are updated.

    Let me know if you find any problems.

    Code:
    ;; Joe Burke - revised version 3/20/2005
    ;; Import dimstyles which don't exist in the active file.
    ;; Update dimstyles in the active file which exist in both files.
    ;; Note: existing associated dimensions will be disassociated.
    (defun c:ImportDimStyles (/ *error* GetDoc doc targstyles oldcnt fullpath
    srcdoc srcstyles actdimstyle name exstnamelst
    srcnamelst copylst endnamelst duplst
    tempstyle namelst newcnt)
    
    ;;local functions
    ;;-------------------------------------------------
    (defun *error* (error)
    (cond
    ((not error))
    ((wcmatch (strcase error) "*QUIT*,*CANCEL*"))
    (T (princ (strcat "\nERROR: " error)))
    )
    (if
    (and
    srcdoc
    (not (vlax-object-released-p srcdoc))
    )
    (vlax-release-object srcdoc)
    )
    (if
    (and
    tempstyle
    (not (vlax-erased-p tempstyle))
    )
    (vla-delete tempstyle)
    )
    (vla-EndUndoMark doc)
    (princ)
    ) ;end error
    
    ;Returns either a doc from the documents collection
    ;or an ObjectDBX doc.
    ;Note, versions earlier than 2004 need modified code
    ;if axdb15.dll is not registered.
    (defun GetDoc ( path / *acad* documents srcdoc )
    (setq *acad* (vlax-get-acad-object)
    documents (vla-get-documents *acad*)
    )
    ;check the documents collection
    (vlax-for x documents
    (if (= path (vlax-get x 'FullName))
    (setq srcdoc x)
    )
    )
    ;or use ObjectDBX
    (if (null srcdoc)
    (progn
    (setq srcdoc
    (vla-GetInterfaceObject *acad* "ObjectDBX.AxDbDocument.16")
    )
    (vla-open srcdoc path)
    )
    )
    srcdoc
    ) ;end
    ;;-------------------------------------------------
    
    (setq doc (vla-get-activedocument (vlax-get-acad-object)))
    (vla-EndUndoMark doc)
    (vla-StartUndoMark doc)
    
    (setq targstyles (vla-get-DimStyles doc)
    oldcnt (vlax-get targstyles 'Count)
    fullpath (getfiled "Select Source File"
    (getvar "dwgprefix") "dwg;dwt;dws" 0)
    )
    
    (if (not fullpath)
    (progn
    (princ "\nSource document not selected. Exiting... ")
    (exit)
    )
    )
    
    (setq srcdoc (GetDoc fullpath)
    srcstyles (vla-get-dimstyles srcdoc)
    actdimstyle (vlax-get doc 'ActiveDimStyle)
    )
    
    ;list the dimstyle names in each document and determine
    ;what the name list in target doc should be after import
    (vlax-for x targstyles
    (setq exstnamelst (cons (vlax-get x 'Name) exstnamelst))
    )
    (vlax-for x srcstyles
    (setq copylst (cons x copylst))
    (setq name (vlax-get x 'Name))
    (setq srcnamelst (cons name srcnamelst))
    (if (not (vl-position (strcase name) (mapcar 'strcase exstnamelst)))
    (setq endnamelst (cons name endnamelst))
    (setq duplst (cons name duplst))
    )
    )
    (setq endnamelst (append exstnamelst endnamelst))
    
    ;rename dimstyles to be updated
    (foreach x duplst
    (vlax-put (vla-item targstyles x) 'Name (strcat "%%" x))
    )
    
    ;copy all source doc dimstyles to target doc
    (vlax-invoke srcdoc 'CopyObjects copylst targstyles)
    
    (vlax-release-object srcstyles)
    (vlax-release-object srcdoc)
    
    (if duplst
    (progn  ;------------
    
    ;add a temporary dimstyle and make it the active dimstyle
    (setq tempstyle (vlax-invoke targstyles 'Add "tempdimensionstyle"))
    (vlax-put doc 'ActiveDimStyle tempstyle)
    
    ;copy dimstyle settings from source style to target style
    (foreach x duplst
    (vla-CopyFrom
    (vla-item targstyles (strcat "%%" x))
    (vla-item targstyles x)
    )
    ;rename each temporary style
    (vlax-put (vla-item targstyles x) 'Name (strcat "##" x))
    )
    
    ;update duplicate names list
    (setq duplst (mapcar '(lambda (a) (strcat "##" a)) duplst))
    
    ;first delete temp substyles
    (vlax-for x targstyles
    (setq name (vlax-get x 'Name))
    (if
    (and
    (vl-position name duplst)
    (vl-string-search "$" name)
    )
    (vl-catch-all-apply 'vla-delete (list x))
    )
    )
    
    ;then delete temp parent styles
    (vlax-for x targstyles
    (setq name (vlax-get x 'Name))
    (if (vl-position name duplst)
    (vl-catch-all-apply 'vla-delete (list x))
    )
    )
    
    ;rename styles to original names
    (vlax-for x targstyles
    (setq name (vlax-get x 'Name))
    (if (vl-string-search "%%" name)
    (vlax-put x 'Name (vl-string-left-trim "%" name))
    )
    )
    
    ;update existing dimensions with new style
    ;settings in Model space and layouts
    (if (ssget "X" (list (cons 0 "DIMENSION")))
    (vlax-for x (vlax-get doc 'ActiveSelectionSet)
    (vla-update x)
    )
    )
    
    ;restore the active dimstyle
    (vlax-put doc 'ActiveDimStyle actdimstyle)
    ;delete the temporary dimstyle
    (vl-catch-all-apply 'vla-delete (list tempstyle))
    
    ) ;progn ------------
    ) ;if
    
    ;Reports:
    ;get the updated list of dimstyle names and compare
    ;with anticipated list: endnamelst
    (vlax-for x targstyles
    (setq namelst (cons (vlax-get x 'Name) namelst))
    )
    
    ;option
    ;(textscr)
    
    (if (equal (acad_strlsort namelst) (acad_strlsort endnamelst))
    (princ "\nImport operation was successful. ")
    (princ "\nImport encountered a problem. Undo or check dimstyles. ")
    )
    
    (if (< oldcnt (setq newcnt (vlax-get targstyles 'Count)))
    (princ (strcat "\nNumber of dimstyles added: " (itoa (- newcnt oldcnt))))
    )
    
    (if duplst
    (princ (strcat " Number of dimstyles updated: " (itoa (length duplst))))
    )
    
    (*error* nil)
    ) ;end
    
    ;; -----------------------------
    ;; shortcut
    (defun c:IDS () (c:ImportDimStyles))
    ;; -----------------------------
    
     
    Joe Burke, Mar 21, 2005
    #20
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.