I seem to have a fundamental misunderstanding of how entget and entmod work. I have 2 lines of code that individually do what they are supposed to do, but one line always reverses what the other did.
The general problem:
A text item currently has a height of 2.5 and a width factor of 0.9. I would like to change this to a height of 2.0 and width factor of 0.8
My code, with line numbers added for clarity:
line 1: (setq shawn-properties (entget shawn-item)) ;get the properties of the text item in question
line 2: (setq shawn-properties-height (assoc 40 shawn-properties)) ;get the current height of the selected text object
line 3: (setq shawn-properties-width (assoc 41 shawn-properties)) ;get the current width factor of the selected text object
line 4: (entmod (subst (cons 40 2.0) shawn-properties-height shawn-properties)) ;replace the current height with 2.0
line 5: (entmod (subst (cons 41 0.8) shawn-properties-width shawn-properties)) ;replace the current width factor with 0.8
Lines 4 and 5 are where it starts to fall apart.
After running line 4, the text height changes to 2.0. That's exactly what I want. So far so good.
After running line 5, the text changes back to height 2.5, but then it correctly changes the width factor to 0.8. It did a good job of fixing the width, but it somehow reversed the height change from line 4. What's going on here?
Trying to go through it one step at a time makes me even more confused. After I run line 4, the text item shrinks to a height of 2.0, but typing "!shawn-properties" still shows a pair of (40 . 2.5). I seem to be misunderstanding what (entmod) changes. Why does the text item change, but the (entget) list for that text item does not change? If I enter line 1 again after the text height has changed, !shawn-properties correctly shows the new height.
The bandaid fix to my problem is to repeat line 1 after line 4, so it looks like this:
line 1: (setq shawn-properties (entget shawn-item)) ;get the starting properties of the text item in question
line 2: (setq shawn-properties-height (assoc 40 shawn-properties)) ;get the current height of the selected text object
line 3: (setq shawn-properties-width (assoc 41 shawn-properties)) ;get the current width factor of the selected text object
line 4: (entmod (subst (cons 40 2.0) shawn-properties-height shawn-properties)) ;replace the current height with 2.0
line 1: (setq shawn-properties (entget shawn-item)) ;get the properties of the text item in question after the height has been changed
line 5: (entmod (subst (cons 41 0.8) shawn-properties-width shawn-properties)) ;replace the current width factor with 0.8
Any insight on entmod and entget would be appreciated. I can't seem to tell when I'm changing an object or just changing a copy of that object's properties.
Solved! Go to Solution.
Solved by MattPalan2009. Go to Solution.
An (entget ...) list isn't dynamically linked to the entity you read it from. Have a simpler check - grab an entity and extract the DXF data into a global (unassigned) variable. Inspect the content. Make some manual changes - layer, color, whatever. Inspect the variable contents.
For your code, you are assigning "old" data to the entity the second time through. Make all changes on the DXF list, then assign it back to the entity *once*.
Just to re-iterate what dgorsman said in a different way.
In line 4 when you do the first entmod you need to save that function to a variable. And then do the second entmod from that variable.
Or do the subst function save it to a variable then the second subst then the ent mod.
line 4: (entmod (subst (cons 40 2.0) shawn-properties-height shawn-properties)) ;replace the current height with 2.0
line 1: (setq shawn-properties (entget shawn-item)) ;get the properties of the text item in question after the height has been changed
line 5: (entmod (subst (cons 41 0.8) shawn-properties-width shawn-properties)) ;replace the current width factor with 0.8
(entmod (subst (cons 41 0.8) shawn-properties-width (subst (cons 40 2.0) shawn-properties-height shawn-properties)))
Matthew