TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: CAB on January 02, 2006, 01:11:26 AM

Title: Change the Dim Style Text Style?
Post by: CAB on January 02, 2006, 01:11:26 AM
Can it be that difficult? :-o
Cant seem to find a visual lisp example.

I want to change the Text style of a particular Dimension style.

This obviously doesn't work but it's as far as I got.
I don't know how to get at the stylename property.

Code: [Select]
(vlax-for itm  (vla-get-dimstyles (vla-get-activedocument (vlax-get-acad-object)))
    (if (= (vla-get-stylename itm) oldsty)
          (vla-put-stylename itm newsty)
    )
)
Title: Re: Change the Dim Style Text Style?
Post by: Kerry on January 02, 2006, 03:32:02 AM
Hi Mr B.
Have a play with something like this ..

Whatever is in the pair List become the overwritten dimvars for the definition based on current Style ..
so you may have to use (vla-put-activedimstyle ... or (vla-get-activedimstyle...

Code: [Select]
(setq g:activedocument (vla-get-activedocument (vlax-get-acad-object))
      g:dimstyles      (vla-get-dimstyles g:activedocument)
)


(Make-Vla-DimStyle (vla-get-name (vla-get-activedimstyle g:activedocument))
                   (list (list "DIMTXSTY" "NORMAL") (list "DIMDEC" 2))
)

Code: [Select]
(defun Make-Vla-DimStyle (StyleName PairList / oDimStyle)
;; codehimbelonga kwb
  (cond
    (DotPairList
     (mapcar '(lambda (var) (vla-setvariable g:activedocument (car var) (cadr var))) PairList )
     (setq oDimStyle (vla-add g:dimstyles StyleName))
     (vla-copyfrom oDimStyle g:activedocument)
     (vla-put-activedimstyle g:activedocument oDimStyle)
    )
    (t (setq oDimStyle (vla-add g:dimstyles StyleName)))
  )
  oDimStyle
)

Title: Re: Change the Dim Style Text Style?
Post by: CAB on January 02, 2006, 11:34:58 AM
Thanks Kerry.

More help needed..
This is my test set up.
It is creating a dimStyle override & then changing the text style.
So it works except for setting the Dim Style current.


Code: [Select]
(defun c:test ()

  (setq *doc* (vla-get-activedocument (vlax-get-acad-object)))

  (setq olstyl "Alan1" ; name to change
        nusty  "A5-8"  ; name to change to
  )

  ;;  step through all dimstyles
  ;;  set dimstyle active
  ;;  if a TextStyle match,
  ;;    change the text style in the dimstyle 

  (setq dimstyles-collection (vla-get-dimstyles *doc*))
  (vlax-for itm dimstyles-collection

    ;;set dimstyle current
    ;; - this doesn't work it creates an override
    (vla-put-activedimstyle *doc* itm)

    (if (= (vlax-invoke *doc* 'getvariable "DimTXSTY") olsty)
      ;;use setvar to access current dimension variables
      (vlax-invoke *doc* 'setvariable "DimTXSTY" nusty)
    )

  ) ; vlax-for

)
Title: Re: Change the Dim Style Text Style?
Post by: Joe Burke on January 02, 2006, 12:07:35 PM
The key here is the CopyFrom method which Kerry mentioned. Study help regarding CopyFrom.

You might change the textstyle of an existing dimension which uses the dimstyle in question. Then use that dimension to modify the dimstyle itself using CopyFrom.
Title: Re: Change the Dim Style Text Style?
Post by: CAB on January 02, 2006, 12:47:45 PM
Thanks Joe.

But you guys have me scratching my head on this one. :lol:

I don't want a copy of a DimStyle.

Here is the current test set up:
I have a DWG with several DimStyles & no actual dim objects in the drawing.
The old and new text styles already exist in the drawing.

I want to change the text styles of the DimStyles using visual Lisp.
Seems simple enough.
Step through the DimStyles Collection
Test the Text Style, if a match change it to the new text style.

Problem is the Dim TextStyle is not acceptable via vlisp that I can see.
The work around seems to be to set a DimStyle current & use the SetVar to change the
text style but even that has alluded me. The code I have found to set a Dim Style
current only sets an over ride of that style current.


If I do create a copy of the dimstyle and change the text style I am in the same situation.
The original Dim Style is still unchanged. Do i somehow replace the Original Dim Style
Title: Re: Change the Dim Style Text Style?
Post by: Chuck Gabriel on January 02, 2006, 12:55:33 PM
CAB,

The DimStyle object does not have any methods to access things like the text style or whether the extension lines are on or off.  The only way you can check any of these properties is to set the dimstyle current and check the values of the dimension-related system variables.  The only way to change these properties is to set the dimension-related system variables and then use CopyFrom to copy the values into the desired DimStyle.
Title: Re: Change the Dim Style Text Style?
Post by: LE on January 02, 2006, 01:01:58 PM
Code: [Select]
;;..............................
(defun rcmd-addDimStyle (name / dimStyle)
  (if (not (vl-catch-all-error-p
     (setq
       dimStyle
(vl-catch-all-apply
  'vla-add
  (list (vla-get-dimStyles (rcmd-thisDwg))
name)))))
    (progn
      (vla-copyFrom dimStyle (rcmd-thisDwg))
      (vla-put-activeDimstyle (rcmd-thisDwg) dimStyle))))

I do something like:

  ;; Specifies the text style of the dimension
  (vla-setVariable
    dtt_thisdwg
    "DIMTXSTY"
    (vlax-make-variant
      (strcat :rcmTextStylePrefix "-" (car factores))
      vlax-vbString))

Then I force the update by calling:

(rcmd-adddimstyle
    (strcat :rcmdimtextstyleprefix "-" (car factores)))

HTH
Title: Re: Change the Dim Style Text Style?
Post by: CAB on January 02, 2006, 01:26:21 PM
Thanks Chuck & Luis.

I'm getting closer to understanding how it works.

I'll be back. :-D
Title: Re: Change the Dim Style Text Style?
Post by: Kerry on January 02, 2006, 03:19:03 PM
CAB,

The DimStyle object does not have any methods to access things like the text style or whether the extension lines are on or off.  The only way you can check any of these properties is to set the dimstyle current and check the values of the dimension-related system variables.  The only way to change these properties is to set the dimension-related system variables and then use CopyFrom to copy the values into the desired DimStyle.

.. I should have said that explicitly ..


Hi CAB, Looks like you're in pretty good hands here .. :-)

As Joe indicated, reading up on CopyFrom Method is the core to the solution.
Title: Re: Change the Dim Style Text Style?
Post by: CAB on January 02, 2006, 04:35:57 PM
Kerry, Yes I didn't realize until Chuck pointed it out what you were alluding to.

Man I'm pulling my hair out, what I have left that is.

I got this to work. (see support functions below)

I set one DimStyle current with overrides, not the target style.
It changes the target style text style fine & the overrides are gone.

Code: [Select]
(defun c:test ( / olsty nusty itm olddim)
  (setq olddim (getvar "DimStyle"))

  (setq *doc* (vla-get-activedocument (vlax-get-acad-object)))

  (setq olsty "ALAN1" ; name to change
        nusty  "A5-8" ; name to change to
  )

  (foreach itm (Table "DimStyle")
     (command "-dimstyle" "R" itm) ; set current

    (if (= (vlax-invoke *doc* 'getvariable "DimTXSTY") olsty)
      (progn
        (vlax-invoke *doc* 'setvariable "DimTXSTY" nusty)
        (updatedimstyle itm)
      )
    )
  )
  (command "-dimstyle" "R" olddim) ; set current
)


But this is what I was after and still wont behave.
It adds the over rides to all the Dimstyles which i don't want.

Code: [Select]
(defun c:test2 ( / olsty nusty itm olddim)
  (setq olddim (getvar "DimStyle"))
  (setq *doc* (vla-get-activedocument (vlax-get-acad-object)))

  (setq olsty "ALAN1" ; name to change
        nusty  "A5-8" ; name to change to
  )

  (setq dimstyles-collection (vla-get-dimstyles *doc*))
 
  (vlax-for itm dimstyles-collection

    ;; set dimstyle current <--<<<  The problem starts here
    (vla-copyfrom itm *doc*)
    (vla-put-activedimstyle *doc* itm)

    (if (= (vlax-invoke *doc* 'getvariable "DimTXSTY") olsty)
      (progn
        ;;use setvar to access current dimension variables
        (vlax-invoke *doc* 'setvariable "DimTXSTY" nusty)
        (setq a (getvar "DimTXSTY"))
        (updatedimstyle (vla-get-name itm))
      )
    )
  ) ; vlax-for
  ;;  restore current dimstyle
  (command "-dimstyle" "R" olddim) ; set current
)

                                   
                                       
Code: [Select]
;;  Support Functions

;Written By Michael Puckett.           
(defun Table (s / d r)                 
  (while (setq d (tblnext s (null d))) 
    (setq r (cons (cdr (assoc 2 d)) r))
  )                                     
)                                       


;;  from Luis
(defun updatedimstyle (name / dimstyle)
  (if (not (vl-catch-all-error-p
             (setq dimstyle (vl-catch-all-apply 'vla-add
                      (list (vla-get-dimstyles *doc*) name))
             )
           )
      )
    (progn
      (vla-copyfrom dimstyle *doc*)
      (vla-put-activedimstyle *doc* dimstyle)
    )
  )
)
Title: Re: Change the Dim Style Text Style?
Post by: Kerry on January 02, 2006, 05:09:23 PM
Can you post a drawing ?
With the TextStyles and DimStyles defined < prior to the Changes you want>

In the mean time :
Make sure the current Active Dimstyle and DimTXSTY is different to "ALAN1"

Try this, one line at a time .... but first change the DimStyle name String to one that that has "ALAN1" as DimTXSTY
Code: [Select]
(setq g:activedocument (vla-get-activedocument (vlax-get-acad-object))
      g:dimstyles      (vla-get-dimstyles g:activedocument)
)
(setq DimStyle (vla-item g:dimstyles "ISO-25")) ;; <-- change Name to Suit

(getvar "DIMTXSTY")
 
(vla-put-activedimstyle g:activedocument DimStyle )

(getvar "DIMTXSTY")
Title: Re: Change the Dim Style Text Style?
Post by: Kerry on January 02, 2006, 05:18:26 PM
Can you confirm what you want .. this is my take on it ..

Progressively Restore each DimStyle Exactly as defined/saved without over-rides.
 and test each .. If the DimTextStyle is "ALAN1" change it to "A5-8", move along.
Title: Re: Change the Dim Style Text Style?
Post by: Kerry on January 02, 2006, 05:44:01 PM
How about this ?
Code: [Select]
(setq g:activedocument (vla-get-activedocument (vlax-get-acad-object))
      g:dimstyles      (vla-get-dimstyles g:activedocument)
)


(defun c:test_k3
       (/ ODIMSTYLE ORIGINALDIMSTYLE STYLENAMEREPLACEMENT STYLENAMETORENAME)
  (setq originalDimStyle     (vla-get-activedimstyle g:activedocument)
        StyleNameToRename    "ROMAND"                  ; name to change
        StyleNameReplacement "DIMS"                    ; name to change to
  )
  (vlax-for eachItem g:dimstyles
    (vla-put-activedimstyle g:activedocument eachItem)
    (if (= (vlax-invoke g:activedocument 'getvariable "DimTXSTY") StyleNameToRename)
      (progn
        (vlax-invoke g:activedocument 'setvariable "DimTXSTY" StyleNameReplacement)
        (setq oDimStyle
               (vla-add g:dimstyles
                        (vla-get-name (vla-get-activedimstyle g:activedocument))
               )
        )
        (vla-copyfrom oDimStyle g:activedocument)
        (vla-put-activedimstyle g:activedocument oDimStyle)
      )
    )
  )
  (vla-put-activedimstyle g:activedocument originalDimStyle)
(princ)
)
Title: Re: Change the Dim Style Text Style?
Post by: CAB on January 02, 2006, 06:05:19 PM
Kerry,
Just returned.
That lisp changed the first one but not the others.
See attached DWG, working in ACAD2000.

Thanks
I'll do some more testing.
Title: Re: Change the Dim Style Text Style?
Post by: Kerry on January 02, 2006, 06:21:16 PM
Well, that's got me stumped ... It works for me on your Drawing.
Title: Re: Change the Dim Style Text Style?
Post by: CAB on January 02, 2006, 06:31:33 PM
SHX files etc.
Title: Re: Change the Dim Style Text Style?
Post by: Kerry on January 02, 2006, 06:41:59 PM
Well, that's got me stumped ... It works for me on your Drawing.

I'll qualify that .. I used AC2006, 'cause I dont have AC2000. I wonder If thats an issue ?

PS : I used a complete ActiveX solution, assuming that was your requirements.
Title: Re: Change the Dim Style Text Style?
Post by: CAB on January 02, 2006, 06:53:49 PM
This line does not set the current dimstyle   
Code: [Select]
    (vla-put-activedimstyle g:activedocument eachItem)therefor this test fails because the DimTXSTY never changes.
Code: [Select]
    (if (= (vlax-invoke g:activedocument 'getvariable "DimTXSTY") StyleNameToRename)
Must be a ACAD2000 bug. Don't sweat it. I have the command line version to fall back on.

I'll be installing ACAD2006 this week so I'll be leaving 2000 behind.

Thanks for the help. :-)
Title: Re: Change the Dim Style Text Style?
Post by: Kerry on January 02, 2006, 06:55:25 PM
My pleasure Alan.