View Full Version : Vl commands to convert text to Mtext
ccowgill
2006-03-02, 06:03 PM
I want to be able to take a piece of text that is blue, convert it to mtext, and add a string that is red to the text that is already there.
Here is what I have so far:
(defun c:cC (/ ss ename obj)
(vl-load-com)
(setq ss (ssget (list (cons 0 "TEXT,attdef,mtext"))))
; removed mtext, %%u won't work on mtext
(and ss ; must test for nil
(setq i -1)
(while (setq ename (ssname ss (setq i (1+ i))))
(setq obj (vlax-ename->vla-object ename))
(vla-put-textstring
obj
(strcat (vla-get-textstring obj) " ({C1;ABANDON})")
)
(vlax-release-object obj)
)
)
(princ)
)
T.Willey
2006-03-02, 06:49 PM
Try this.
(defun c:Test (/ ss Ent obj TxtObj)
(vl-load-com)
(if (setq ss (ssget (list (cons 0 "TEXT,attdef,mtext"))))
; removed mtext, %%u won't work on mtext
(while (setq Ent (ssname ss 0))
(setq obj (vlax-ename->vla-object Ent))
(if
(or
(= (vla-get-ObjectName obj) "AcDbText")
(= (vla-get-ObjectName obj) "AcDbAttributeDefinition")
)
(progn
(setq TxtObj
(vla-AddMtext
(vlax-get
(vla-get-ActiveDocument (vlax-get-Acad-Object))
(if (equal (getvar "cvport") 1)
'PaperSpace
'ModelSpace
)
)
(if (= (vla-get-Alignment obj) 0)
(vla-get-InsertionPoint obj)
(vla-get-TextAlignmentPoint obj)
)
0
(if (= (vla-get-ObjectName obj) "AcDbText")
(vla-get-TextString obj)
(vla-get-TagString obj)
)
)
)
(foreach x '(Layer Height Rotation)
(vlax-put TxtObj x (vlax-get obj x))
)
(vla-Delete obj)
(setq obj TxtObj)
)
)
(ssdel Ent ss)
(vla-put-textstring
obj
(strcat (vla-get-textstring obj) " ({\\C1;ABANDON})")
)
(vlax-release-object obj)
)
)
(princ)
)
ccowgill
2006-03-02, 06:54 PM
That gives me a great start, I will mess around with it and see if I can get it to keep the current justification and insertion, If I cant figure it out, I'll post another question.
Thanks,
T.Willey
2006-03-02, 07:07 PM
It should put the mtext in at the correct insertion point, but the insertion point is referenced different for the mtext. You will have to make a cond statement to test for all the justification, and then match them to the mtext equivalence.
You're welcome.
ccowgill
2006-03-02, 07:34 PM
(setq TxtObj
(vla-AddMtext
(vlax-get
(vla-get-ActiveDocument (vlax-get-Acad-Object))
(if (equal (getvar "cvport") 1)
'PaperSpace
'ModelSpace
)
)
(if (= (vla-get-Alignment obj) 0)
(vla-get-InsertionPoint obj)
(vla-get-TextAlignmentPoint obj)
)
0 <- What is this zero for?
(if (= (vla-get-ObjectName obj) "AcDbText")
(vla-get-TextString obj)
(vla-get-TagString obj)
)
)
)
How and where would I set up the conditional statements. I suppose I would use and if statement to match the two alignment points up
would It look somehting like this:
(setq jp (vla-get-alignment txtObj)
);end of setq
(if (= jp acAlignmentMiddleLeft)
(setq na "ml")
(command "_.justifytext" Obj "" na)
T.Willey
2006-03-02, 07:43 PM
The 0 is for the mtext width, so that it all fits on one line.
I would set up the cond statement after the new text object get created. The old text object is still around, so you can still get the info from it, but the alignment (justification) is different for text and mtext objects. I haven't done all the research on them, so that is something you will have to do, but you don't need a command to change the alignment of the object, just change the alignment property.
You cond should looks something like
(cond
((equal (vla-get-Alignment obj) 0)
(vla-put-Alignment TxtObj 1)
)
... and so on....
)
On a side note, you might have to set the text alignment point of the mtext object once you change the alignment, I know you have to with the text objects, but I'm not sure about mtext object since I hate mtext, and try not to use whenever I can.
ccowgill
2006-03-03, 06:06 PM
I am having trouble converting insertions from text to mtext. I can get the insertion point to change, but I cant get it to place the text where I want, it moves the insertionpoint instead of moving the mtext
T.Willey
2006-03-03, 06:40 PM
I will look into this when I have some time. Maybe someone who uses mtext will chime in, and maybe they will have some code to help.
T.Willey
2006-03-03, 07:36 PM
This seems to work for me.
(defun c:Test (/ ss Ent obj TxtObj InsPt)
(vl-load-com)
;-----------------------------------------------
(defun MtMatchDtAlignment (DtObj / DtAli)
(setq DtAli (vla-get-Alignment DtObj))
(cond
((or
(equal DtAli 0)
(equal DtAli 3)
(equal DtAli 5)
(equal DtAli 12)
)
7
)
((or
(equal DtAli 1)
(equal DtAli 13)
)
8
)
((or
(equal DtAli 2)
(equal DtAli 14)
)
9
)
((or
(equal DtAli 4)
(equal DtAli 10)
)
5
)
((equal DtAli 6)
1
)
((equal DtAli 7)
2
)
((equal DtAli 8)
3
)
((equal DtAli 9)
4
)
((equal DtAli 11)
6
)
)
)
;----------------------------------------------------------------------
(vla-StartUndoMark (vla-get-ActiveDocument (vlax-get-Acad-Object)))
(if (setq ss (ssget (list (cons 0 "TEXT,attdef,mtext"))))
; removed mtext, %%u won't work on mtext
(while (setq Ent (ssname ss 0))
(setq obj (vlax-ename->vla-object Ent))
(if
(or
(= (vla-get-ObjectName obj) "AcDbText")
(= (vla-get-ObjectName obj) "AcDbAttributeDefinition")
)
(progn
(setq TxtObj
(vla-AddMtext
(vlax-get
(vla-get-ActiveDocument (vlax-get-Acad-Object))
(if (equal (getvar "cvport") 1)
'PaperSpace
'ModelSpace
)
)
(setq InsPt
(if (= (vla-get-Alignment obj) 0)
(vla-get-InsertionPoint obj)
(vla-get-TextAlignmentPoint obj)
)
)
0
(if (= (vla-get-ObjectName obj) "AcDbText")
(vla-get-TextString obj)
(vla-get-TagString obj)
)
)
)
(foreach x '(Layer Height Rotation)
(vlax-put TxtObj x (vlax-get obj x))
)
(vla-put-AttachmentPoint TxtObj (MtMatchDtAlignment obj))
(vla-put-InsertionPoint TxtObj InsPt)
(vla-Delete obj)
(setq obj TxtObj)
)
)
(ssdel Ent ss)
(vla-put-textstring
obj
(strcat (vla-get-textstring obj) " ({\\C1;ABANDON})")
)
(vlax-release-object obj)
)
)
(vla-EndUndoMark (vla-get-ActiveDocument (vlax-get-Acad-Object)))
(princ)
)
ccowgill
2006-03-03, 07:46 PM
That works great, thanks Tim for all your help.
T.Willey
2006-03-03, 07:54 PM
You're welcome.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.