Jump to content

Expand a text routine to include MTEXT/DIM


Recommended Posts

Posted

I have a routine that adds parenthesis around text objects.

I don't know how to modify this code to include MTEXT, DIMENSIONS & MULTILEADER objects. (Any other text type objects??)

 

I would love to have a more efficient coded version but first I'd like

to see how my code would be modified to include these other objects.

(I'm still learning..)

 

(DEFUN c:PT (/ ss len count ent text)

(SETVAR "CMDECHO" 0)

(COMMAND "UNDO" "M")

(PROMPT "\nAdds Parenthesis round selected text: ")

(setq olderr *error*

*error* ULERR)

(PROMPT "\nSelect text: ")

(SETQ SS (SSGET)

LEN (SSLENGTH SS)

COUNT 0)

(WHILE (NOT (EQUAL LEN COUNT))

(SETQ ENT (ENTGET (SSNAME SS COUNT)))

(IF (EQUAL (CDR (ASSOC 0 ENT)) "TEXT")

(PROGN

(SETQ TEXT (CDR (ASSOC 1 ENT))

TEXT (STRCAT "(" TEXT ")" )

ENT (SUBST (CONS 1 TEXT)

(ASSOC 1 ENT) ENT))

(ENTMOD ENT)))

(SETQ COUNT (1+ COUNT)))

(PRINC))

 

Thank you guys again for not only what you've helped me with

but for all the code you write! It's a bit overwhelming...

 

Thanks again!!

Posted
I have a routine that adds parenthesis around text objects.

 

I would use something like this

 

(defun c:atp  (/ count en ent len ss text)

 (command "._undo" "_be")
 
 (prompt "\nAdds parenthesis round selected text: ")
 
 (setq	ss    (ssget (list (cons 0 "*TEXT,DIMENSION,MULTILEADER")))
len   (sslength ss)
count 0)
 (while (not (equal len count))
   (setq ent (entget (setq en (ssname ss count))))
   (cond
     ((wcmatch (cdr (assoc 0 ent)) "*TEXT")
      (setq text (cdr (assoc 1 ent))
     text (strcat "(" text ")")
     ent  (subst (cons 1 text)
		 (assoc 1 ent)
		 ent)))

     ((equal (cdr (assoc 0 ent)) "DIMENSION")
      (progn
 (setq text (cdr (assoc 1 ent))
       text "(<>)"
       ent  (subst (cons 1 text)
		   (assoc 1 ent)
		   ent))
 ))
     ((equal (cdr (assoc 0 ent)) "MULTILEADER")
      (progn
 (setq text (cdr (assoc 304 ent))
       text (strcat "(" text ")")
       ent  (subst (cons 304 text)
		   (assoc 304 ent)
		   ent))
 ))
     )
   (entmod ent)
   (setq count (1+ count))
   )
 (command "._undo" "_e")
 
 (princ)
 )

 

~'J'~

Posted

Thanks!!

 

Works Perfect!!!

 

Now how about the high-end coded version in v-lisp or C#?

 

It's a good learning tool to be able to compare simple lisp to

the other language versions.

 

Thanks so much!!!

Posted

Thanks!

 

If you get a chance it would be appreciated!!

We use parenthesis around dimension to designate that they are approximate.

(It's their standard) Everwhere else we used a tilde.

 

I don't get to make the rules...

I just have to draft by them!! (^_^)

 

I'll try to figure out how to add the dimensions.

If I can't figure it out I'll bug you again...

 

Thank you again!

Posted
Thanks!!

 

Works Perfect!!!

 

Now how about the high-end coded version in v-lisp or C#?

 

It's a good learning tool to be able to compare simple lisp to

the other language versions.

 

Thanks so much!!!

 

Here is VLisp version too

(defun C:Par (/ *error* adoc axss obj_type)
 
(defun *error*	(msg)
 (if
   (vl-position
     msg
     '("console break"
"Function cancelled"
"quit / exit abort"
)
     )
    (princ "Error!")
    (princ msg))
   (vla-startundomark
     (vla-get-activedocument
       (vlax-get-acad-object)
     )
     )
    )
 
 ;;main part

     (setq adoc
     (vla-get-activedocument
       (vlax-get-acad-object)
     )
     ) 
 (vla-startundomark adoc)
 
 (setq axss (vla-get-pickfirstselectionset adoc))
 (vla-clear axss)
 (vla-selectonscreen
   axss
   (vlax-safearray-fill
     (vlax-make-safearray vlax-vbinteger '(0 . 4))
     '(-4 0 0 0 -4))
   (vlax-safearray-fill
     (vlax-make-safearray vlax-vbvariant '(0 . 4))
     '("<OR" "*TEXT" "DIMENSION" "MULTILEADER" "OR>")
     )
   )

 (vlax-for obj axss
   (setq obj_type (vla-get-objectname obj))
   (cond ((wcmatch obj_type "*Dimension")
   (vla-put-textoverride obj "(<>)"))
  ((wcmatch obj_type "*Leader,*Text")
           (vla-put-textstring obj (strcat "(" (vla-get-textstring obj) ")"))
   )
  )
   )
   (princ)
 )
(vl-load-com)

 

This would be pretty easy to write it on C# or VB.NET

Here is a good point to start:

http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html

 

~'J'~

Posted
Thanks!!

 

Works Perfect!!!

 

Now how about the high-end coded version in v-lisp or C#?

 

It's a good learning tool to be able to compare simple lisp to

the other language versions.

 

Thanks so much!!!

 

Here is C# version based on Adesk guide

Tested slightly

 

using System;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.ApplicationServices;


[assembly: CommandClass(typeof(SelectionCommands.FilterSelect))]

namespace SelectionCommands
{
   public class FilterSelect
   {
       [CommandMethod("FM")]
       public static void AddParenthesis()
       {
           // Get the current document
           Document doc = acadApp.DocumentManager.MdiActiveDocument;
           // Get the current document editor
           Editor ed = doc.Editor;
           // Get the current database
           Database db = doc.Database;
           using (Transaction tr = db.TransactionManager.StartTransaction())
           {
               // Create a TypedValue array to define the filter criteria

               TypedValue[] ftype = 
                   new TypedValue[] 
{ 
   new TypedValue((int)DxfCode.Operator, "<OR"), 
   new TypedValue((int)DxfCode.Start, "*TEXT"),//Text and MText
   new TypedValue((int)DxfCode.Start, "DIMENSION"),
   new TypedValue((int)DxfCode.Start, "*LEADER"),//Leader and MLeader
   new TypedValue((int)DxfCode.Operator, "OR>") 
};
               // Assign the filter criteria to a SelectionFilter object
               SelectionFilter fltr = new SelectionFilter(ftype);

               // Request for objects to be selected in the drawing area
               PromptSelectionResult psr;
               psr = ed.GetSelection(fltr);
               SelectionSet sset = null;
               // If the prompt status is OK, objects were selected
               if (psr.Status == PromptStatus.OK)
               {
                   sset = psr.Value;

                   acadApp.ShowAlertDialog("Number of objects selected: " +
                                               sset.Count.ToString());
               }
               else
               {
                   acadApp.ShowAlertDialog("Number of objects selected: 0");
               }
               // iterate through selected objects
               foreach (ObjectId id in sset.GetObjectIds())
               {
                   DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForRead);
                   // catch object name
                   string objname = obj.GetRXClass().Name;

                   ed.WriteMessage("\n{0}", objname);// debug only

                   try
                   {
                       switch (objname)
                       {
                           case "AcDbText":
                               DBText txt = obj as DBText;
                               txt.UpgradeOpen();
                               txt.TextString = "(" + txt.TextString + ")";
                               break;
                           case "AcDbMText":
                               MText mtx = obj as MText;
                               mtx.UpgradeOpen();
                               mtx.Contents = "(" + mtx.Contents + ")";
                               break;
                           case "AcDbLeader":
                               Leader ld = obj as Leader;
                               if (ld.AnnoType == AnnotationType.MText)
                               {
                                   MText ldtxt = (MText)tr.GetObject(ld.Annotation, OpenMode.ForRead);
                                   if (ldtxt != null)
                                   {
                                       ld.UpgradeOpen();
                                       ldtxt.UpgradeOpen();
                                       ldtxt.Contents = "(" + ldtxt.Contents + ")";
                                   }
                               }
                               break;
                           case "AcDbMLeader":
                               MLeader mld = obj as MLeader;

                               if (mld.ContentType == ContentType.MTextContent)
                               {
                                   MText mt = new MText();
                                   mt.SetDatabaseDefaults();
                                   mt.SetPropertiesFrom(mld.MText as Entity);
                                   mt.Contents = "(" + mld.MText.Text + ")";
                                   mld.UpgradeOpen();
                                   mld.MText = mt;
                               }
                               break;
                           //varios object names:
                           //AcDbRotatedDimension
                           //AcDbAlignedDimension
                           //AcDb2LineAngularDimension
                           //AcDbRadialDimension
                           //AcDbDiametricDimension
                           //AcDbOrdinateDimension
                           //AcDbArcDimension
                           case "AcDbRotatedDimension":
                               RotatedDimension rd = obj as RotatedDimension;
                               rd.UpgradeOpen();
                               rd.DimensionText = "(<>)";
                               break;
                           case "AcDbAlignedDimension":
                               AlignedDimension ad = obj as AlignedDimension;
                               ad.UpgradeOpen();
                               ad.DimensionText = "(<>)";
                               break;
                           case "AcDbOrdinateDimension":
                               OrdinateDimension od = obj as OrdinateDimension;
                               od.UpgradeOpen();
                               od.DimensionText = "(<>)";
                               break; //etc. etc.
                       }
                   }
                   catch (Autodesk.AutoCAD.Runtime.Exception ex)
                   {
                       acadApp.ShowAlertDialog(ex.Message);
                   }
               }
               tr.Commit();
           } //dispose transaction
       }
   }
}

 

~'J'~

Posted

Thank you guys so much!!

 

You're AWESOME!!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...