Some observations related to dealing with Associative dimensions ...The DimObj.Measurement property is
NOT necessarily reflected by the displayed Text.
I have seen innumerable "solutions" to extracting the actual dimension which erroneously use the Measurement property.
The 'MText' value actually displayed is buried in the nested entity associated with the -2 key of the anonymous block associated with the 2 key of the "DIMENSION" entity.
By stepping through the nested entitys using ENTNEXT all the entitys 'belonging' to the Dimension can be inspected. The nested entitys may include the LINES, INSERTS, MTEXT, and POINTS which make up a Dimension. Depending on the MText formatting there may be several entrys for MTEXT ; an example would be where "\X" or "\P" formatting is used .. each additional line of text will require an additional entity definition nested in the Dimension.
Figure 01
Result from the Measurement Property
This can be demonstrated with the following code :
(SETQ DimEnt (CAR (ENTSEL "\nSelect dimension :"))
DimObj (VLAX-ENAME->VLA-OBJECT DimEnt)
Measurement (VLAX-GET DimObj 'Measurement)
)
;;Returns-> 550.903
Which is NOT the actual displayed Text Value.
Note also the Value of the following properties :
(VLAX-GET DimObj 'TextPrefix)
;;Returns-> ""
(VLAX-GET DimObj 'TextSuffix)
;;Returns-> ""
(VLAX-GET DimObj 'TextOverride)
;;Returns-> ""
So we write this Code to determine Entity Nesting .. warts and all :
(DEFUN c:UnNest-Dims ()
(IF (AND (SETQ DimEnt (CAR (ENTSEL "\nSelect dimension :")))
(SETQ dent (ENTGET DimEnt '("*")))
(= "DIMENSION" (CDR (ASSOC 0 dent)))
(SETQ anon_block (CDR (ASSOC 2 dent)))
(SETQ nested_ent (CDR (ASSOC -2 (TBLSEARCH "BLOCK" anon_block 0))))
)
(WHILE (SETQ nested_ent (ENTNEXT nested_ent))
(SETQ EntityType (CDR (ASSOC 0 (ENTGET nested_ent))))
(PRINC (STRCAT "\nEntity :" EntityType))
(IF (= "MTEXT" EntityType)
(PRINC (STRCAT " :- " (CDR (ASSOC 1 (ENTGET nested_ent)))))
)
)
)
(PRINC)
)
Result from the nested Entitys using Figure 01 data:
Command: UnNest-Dims
Select dimension :
Entity :LINE
Entity :LINE
Entity :INSERT
Entity :INSERT
Entity :MTEXT :- 551
Entity :POINT
Entity :POINT
Entity :POINT
Entity :POINT
Let's try some more to test the code ;
First, Change the displayed Values ..
(VLAX-PUT DimObj 'TextPrefix "Prefix ")
(VLAX-PUT DimObj 'TextSuffix " Suffix")
Figure 02
Result
Command: UnNest-Dims
Select dimension :
Entity :LINE
Entity :LINE
Entity :INSERT
Entity :INSERT
Entity :MTEXT :- Prefix 551 Suffix
Entity :POINT
Entity :POINT
Entity :POINT
Try some more ;
Change the Suffix ..
(VLAX-PUT DimObj 'TextSuffix " NewSuffix\\Xand Additional Text")
Figure 03 Result
Command: UnNest-Dims
Select dimension :
Entity :LINE
Entity :LINE
Entity :INSERT
Entity :INSERT
Entity :MTEXT :- NewPrefix 551 NewSuffix
Entity :MTEXT :- and Additional Text
Entity :POINT
Entity :POINT
Entity :POINT
Check the Suffix and Prefix
(VLAX-GET DimObj 'TextPrefix)
;;Returns->"NewPrefix "
(VLAX-GET DimObj 'TextSUFFIX)
;;Returns-> " NewSuffix\\Xand Additional Text"
(VLAX-GET DimObj 'TextOverride)
;;Returns-> ""
Try once More ;
(VLAX-PUT DimObj 'TextPrefix " NewPrefix\\Pand Additional Prefix ")
Figure 04 Result
Select dimension :
Entity :LINE
Entity :LINE
Entity :INSERT
Entity :INSERT
Entity :MTEXT :- NewPrefix\Pand Additional Prefix 941 NewSuffix
Entity :MTEXT :- and Additional Text
Entity :POINT
Entity :POINT
So, theotetically, we can extract the 'ACTUAL' Displayed Dimension ...
by Grabbing the concattenating the MText values and removing any part of the prefix and any part of the suffix that are included in the string. { removing any "\\X" and "\\P" formatting first }
.... but it would get very tedious,
SO, Because Acad adds XData for dimension Overrides, we could try this ...
;; (GetDimText-fromXdata)
(DEFUN GetDimText-fromXdata (/ textValue)
(AND (SETQ DimEnt (CAR (ENTSEL "\nSelect dimension :")))
(SETQ acad-dstyle (CADR (ASSOC -3 (ENTGET DimEnt '("ACAD")))))
(SETQ overrides (MEMBER (CONS 1002 "{") acad-dstyle))
(SETQ textValue (CDR (ASSOC 1000 overrides)))
)
textValue
)
Figure 04 Result
Command: (GetDimText-fromXdata)
Select dimension :
" NewPrefix\\Pand Additional Prefix <> NewSuffix\\Xand Additional Text"
Then split the string with something like this ;
(setq xdString (GetDimText-fromXdata))
;;-> " NewPrefix\\Pand Additional Prefix <> NewSuffix\\Xand Additional Text"
(setq splitposition (vl-string-search "<>" xdString))
;;-> 34
(setq pre-position (vl-string-subst " " "\\X" (vl-string-trim " "(substr xdString 1 splitposition))))
;;-> " NewPrefix\\Pand Additional Prefix "
(setq post-position (vl-string-subst " " "\\X" (vl-string-trim " " (substr xdString (+ splitposition 3)))))
;;-> "NewSuffix and Additional Text"
Then Re-Write the UnNest-Dims routine to concatenate the Mtext ;
(DEFUN c:UnNest-Dim-Value (/ ReturnValue)
(IF (AND (SETQ ReturnValue ""
DimEnt (CAR (ENTSEL "\nSelect dimension :"))
)
(SETQ dent (ENTGET DimEnt '("*")))
(= "DIMENSION" (CDR (ASSOC 0 dent)))
(SETQ anon_block (CDR (ASSOC 2 dent)))
(SETQ nested_ent (CDR (ASSOC -2 (TBLSEARCH "BLOCK" anon_block 0))))
)
(WHILE (SETQ nested_ent (ENTNEXT nested_ent))
(IF (= "MTEXT" (CDR (ASSOC 0 (ENTGET nested_ent))))
(SETQ ReturnValue (STRCAT ReturnValue " " (CDR (ASSOC 1 (ENTGET nested_ent)))))
)
)
)
ReturnValue
)
(setq Concatted-Mtext (c:UnNest-Dim-Value ))
;;-> " NewPrefix\\Pand Additional Prefix 941 NewSuffix and Additional Text"
Then finally ...
(setq
dimValue (vl-string-subst "" pre-position Concatted-Mtext)
dimValue (vl-string-trim " " (vl-string-subst "" post-position dimValue))
)
;;-> "941"
;;-> Whheeeee !
This then lets us overRide the Text value so that we keep the original value when we stretch the Dimension Node Points.
(VLAX-PUT DimObj 'TextOverride (VL-STRING-SUBST dimValue "<>" xdString))
.... and that little line of code seems SO simple .....
Figure 05 Result
Note the 941 dimension, overridden from the Figure 4 data
The current values are now ;
(VLAX-GET DimObj 'TextPrefix)
;;-> " NewPrefix\\Pand Additional Prefix "
(VLAX-GET DimObj 'TextSUFFIX)
;;-> " NewSuffix\\Xand Additional Text"
(VLAX-GET DimObj 'TextOverride)
;;-> " NewPrefix\\Pand Additional Prefix 941 NewSuffix\\Xand Additional Text"
So if the Dimension has been Overridden before we start, it's simply a matter of removing the prefix and suffix strings from the Override string and we have the string representing the Dimension Value.
[edit : piccys added ]