Results 1 to 8 of 8

Thread: Lisp to add an MText object that contains an object property

  1. #1
    Login to Give a bone
    0

    Default Lisp to add an MText object that contains an object property

    Hi there,

    I probably decided to tackle something way too complicated for one of my first lisps. I want to create a tool to number piles. The piles that we have are created in Bentley ProStructures, so they are not AutoCAD objects. I have figured out how to set the "PosNumber" property on the pile to whatever the user has entered, but I'm kind of stuck as to how to place an Mtext object that contains that information. My ultimate goal would be to have a tool that allows a user to enter the plant design area prefix, starting number, text angle and text offset once at the beginning, and then allow them to pick any number of piles and have the pile number increment. This information would be displayed in the Mtext, and the insertion point of the text would be X offset (picked by the user) from the centre snap of the pile. I'm prepared to work through the rest of these aspects myself, but I would really appreciate the help on inserting the Mtext. Below is the code I have so far. Also, please feel free to comment if you have suggestions for best practices, as I'm new at this.

    Thanks,
    Sheila

    Code:
    (defun c:postag ()
      (vl-load-com)
      (setvar "cmdecho" 0)
      (setvar "clayer" "TXT-GEN")  
      (setq prefix1 (getstring "\nEnter Design Area Pile Prefix Number (eg. 4010): ")
       count1 (getstring "\nEnter Starting Pile Number (eg. 001): ")
       txtang1 (getangle "\nEnter Text Angle: ")
       offset1 (getint "\nEnter Text Offset: ")
       pilnum1 (strcat prefix1 "-" count1)
      )
      (setq pile1 (car (entsel "\nSelect first pile: ")))
      (setq vpile1 (vlax-ename->vla-object pile1))
      (vlax-put-property vpile1 'posnumber pilnum1)  
      (princ)
    )

  2. #2
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,096
    Login to Give a bone
    0

    Default Re: Lisp to add an MText object that contains an object property

    Do you have a sample dwg?
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

  3. #3
    Login to Give a bone
    0

    Default Re: Lisp to add an MText object that contains an object property

    Here is a basic one. Not sure how the piles will show up for you all w/o a ProStructures enabler....

    pilesaugi.dwg

  4. #4
    Login to Give a bone
    0

    Default Re: Lisp to add an MText object that contains an object property

    Okay, I managed to figure out how to add the mtext and how to get the object property to increment. Now I'm stuck on adding the rotation value for the mtext entered by the user and the insertion offset. Any ideas?

    Code:
    (defun lzero (initval1 n)
    	  (if (< (strlen initval1) n)
    	    (lzero (strcat "0" initval1) n)
    	    initval1
    	  )
    	)
    
    (defun c:postag ()
      (vl-load-com)
      (setvar "cmdecho" 0)
      (setvar "clayer" "TXT-GEN")
      (setq prefix1 (getstring "\nEnter Design Area Pile Prefix Number (eg. 4010): "))
      (setq initval1 (getint "\nEnter Starting Pile Number: "))
      (setq txtr1 (getangle "\nEnter text angle: "))
      (setq toff1 (getint "\nEnter text offset from centre of pile: "))
      (while
      	(setq pile1 (car (entsel "\nSelect Piles (in order): ")))
      	(setq vpile1 (vlax-ename->vla-object pile1))
      	(vlax-put-property vpile1 'posnumber (strcat prefix1 "-" (lzero (itoa initval1) 3)))
    	(setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
      	(setq inspt1 (vlax-get-property vpile1 'InsertPoint))
        	(setq txtins1 (vlax-safearray->list (vlax-variant-value inspt1)))
        	(setq pilnum1 (vlax-get-property vpile1 'posnumber))
        		(vla-addmtext mspace (vlax-3d-point txtins1) 100 pilnum1)
        	(setq initval1 (1+ initval1))
      )
      (princ)
    )

  5. #5
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,096
    Login to Give a bone
    0

    Default Re: Lisp to add an MText object that contains an object property

    You want to look at the rotation property of the MText object. You can dump the properties of a VLA-OBJECT with this code
    Code:
    (VLAX-DUMP-OBJECT {vlaobject})
    You can also see the available methods for the object by adding t to the end of that call.
    Code:
    (VLAX-DUMP-OBJECT {vlaobject} t)
    Of course, replace {vlaobject} in the above codes to a variable containing the vla-object.
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

  6. #6
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: Lisp to add an MText object that contains an object property

    I don't have this object enabler, so the piles do not show properly for me, but there are a few things that you may be able to do to improve your code, for example:

    By wrapping all your prompts for user input in an And expression (as the test expression for an If statement) you require all input to be valid (non-Nil) in order to evaluate the rest of your code. As written, if the user hits escape for one of the prompts, the next prompt is still evaluated, let alone the code that is dependent on the prompt results.

    While it is necessary to define the space Object parameter for the vla-AddMText Method, this only need be done once (prior to the While loop), instead of each time a pile selection is made (inside the While loop).

    You currently prompt the user for both a rotation angle, and offset for the text label you're adding following pile selection, but you're not doing anything with them yet (I get you needed help with that part; more on that below)... But I wanted to point out that currently you're extracting the pile Object's InsertionPoint Property (a variant), converting it to a list or reals, then using vlax-3D-Point to convert it back to a variant, when instead you could simply supply the original variant returned by the InsertionPoint Property.

    You can us Polar to calculate the offset/rotated InsertionPoint to be used for your newly added MText, but in order to do this you need to supply a coordinate list, and not a variant... 'But you just made a point about converting from variant, etc.!?' - and I did, just stay with me... There often a few different means to get or set a particular Object's Property (i.e., vlax-get-property <obj> '<prop> etc.), and more so with the getting of Properties, these different means not only have a slightly different syntax, but more importantly each syntax yields a different result, or returned value.

    Here's a quick example... I'm going to extract the InsertionPoint of a given block Object (VLA), and convert to a coordinate list (a list of real numbers):

    Code:
    (vlax-safearray->list (vlax-variant-value (vla-get-insertionpoint oBlock)))
    ... Or, I could simply extract the property as a coordinate list with:

    Code:
    (vlax-get oBlock 'insertionpoint)
    ... Which is both faster, and less code. Win-win.



    In any event, I hope you find those comments helpful... Here's an untested LISP routine to demonstrate:

    Code:
    (vl-load-com)
    
    (defun c:POSTAG (/ *error* _suffix prefix i ang d acDoc space clayer
                     eName oPile oText
                    )
    
      (defun *error* (msg)
        (and clayer (setvar 'clayer clayer))
        (if acDoc
          (vla-endundomark acDoc)
        )
        (cond ((not msg))                                                   ; Normal exit
              ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
              ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
        )
        (princ)
      )
    
      (defun _suffix (i n)
        (if (< (strlen i) n)
          (_suffix (strcat "0" i) n)
          i
        )
      )
    
      (if
        (and (setq prefix (getstring "\nEnter pile prefix: "))
             (setq i (getint "\nEnter pile start number: "))
             (setq ang (getreal "\nEnter text angle: "))
             (setq d (getreal "\nEnter text offset from center of pile: "))
        )
         (progn
           (vla-startundomark
             (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
           )
           (setq space
                  (vlax-get acDoc
                            (if (= 1 (getvar 'tilemode))
                              'modelspace
                              (if (= 1 (getvar 'cvport))
                                'paperspace
                                'modelspace
                              )
                            )
                  )
           )
           (setq clayer (getvar 'clayer))
           (setvar 'clayer "TXT-GEN")
           (while (and (setq eName (car (entsel)))
                       (setq oPile (vlax-ename->vla-object eName))
                       (vlax-property-available-p oPile 'posnumber)
                  )
             (vlax-put oPile
                       'posnumber
                       (setq n (strcat prefix "-" (_suffix (itoa i) 3)))
             )
             (vla-rotate
               (setq oText
                      (vla-addmtext
                        space
                        (vlax-3d-point
                          (polar (vlax-get oPile 'insertionpoint) ang d)
                        )
                        100.0
                        n
                      )
               )
               (vla-get-insertionpoint oText)
               ang
             )
             (setq i (1+ i))
           )
         )
         (cond (ang (prompt "\n** Must enter text offset ** "))
               (i (prompt "\n** Must enter text angle ** "))
               (prefix (prompt "\n** must enter pile start number ** "))
         )
      )
      (*error* nil)
    )


    Separately, as a matter of drawing maintenance... To keep your files clean, and running quickly, you should make a habit of purging registry applications (scroll down):

    Code:
    Loading AEC Base...
    Loading AEC Base Extended...
    Loading AEC Base UI...
    Loading AEC Project Base...
    Loading AEC Base GUI...
    Loading AEC Schedule Data...
    Loading AEC Project UI...
    Loading AEC Utilities...
    
    Customization file loaded successfully. Customization Group: BLACKBOX
    
    Initializing....
    There are no valid MS Jet providers installed......Done.
    Loading AECC Base...
    Loading AECC Land...
    Loading AECC Subentity Selection...
    
    Loading Modeler DLLs.
    Loading AECC Pipe Part...
    Loading AECC QTO......
    Loading AECC Pipe Network...
    Loading AECC Roadway...
    Loading AECC Survey...
    Loading AEC Schedule...
    Loading AECC Plan Production...
    Loading AEC Architectural Base...
    Loading AEC Structural Base...
    Loading AEC Area Base...
    Loading AEC Dimensions Base...
    Loading AecCivilBase...
    Loading AECC Building Site...
    Loading AECC Point Cloud...
    Regenerating model.
    *Cancel*
    
    Blacklist 1.0.0.0
    Creative innovation by BlackBox, 2012
    
    Loading blacklisted AutoCAD file definition(s):
       acad.fas
       acad.vlx
       acaddoc.fas
       acaddoc.vlx
       bakdwg.fas
       bakdwg.vlx
    
    Blacklist: No blacklisted AutoCAD file(s) detected.
    
    Purged 1153 registry applications from the current database.
    AutoCAD menu utilities loaded.
    
    Loading AECC Hydrology...
    Loading AECC Base UI...
    Loading AECC Event Viewer...
    Loading AECC Land UI...
    Loading AECC QTO UI...
    Loading AECC Pipe Network UI...
    Loading AECC Roadway UI...
    Loading AECC Survey UI...
    Loading AECC Plan Production UI...
    Loading AECC Publish UI...
    Loading AECC AeccUiHydrology...
    Loading AECC Mapcheck...
    Loading AECC Mapcheck UI...
    Loading AECC Building Site UI...
    Loading AECC Point Cloud UI...
    Loading AECC Management UI...
    Loading AECC 2011 SAP UI...
    Loading AECC Model UI...
    
    Loading MgdDbg...
    
    XColor 1.0.0.0
    Creative innovation by BlackBox, 2011
    
    AutoCAD menu utilities loaded.Regenerating model.
    
      >>  Reactors Loaded
    
    AutoCAD menu utilities loaded.
    
    Autodesk DWG.  This file is a TrustedDWG last saved by an Autodesk application 
    or Autodesk licensed application.
    Last edited by BlackBox; 2013-07-22 at 05:55 PM.
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  7. #7
    Login to Give a bone
    0

    Default Re: Lisp to add an MText object that contains an object property

    Thanks for all the comments! They're really helpful. I think I'll take a look at the Polar command to see what I can come up with. At least I'm learning a lot figuring all this out. We do purge out regapps on a regular basis. I'm still amazed at the number of people who are unaware of them...

    BlackBox, your code is returning the error "too few arguments", but I can see the examples of what you've done. Thanks for your help. I was wondering about adding error messages and what not.

    Sheila

  8. #8
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: Lisp to add an MText object that contains an object property

    Quote Originally Posted by sheila.bjerreskov717262 View Post
    Thanks for all the comments! They're really helpful. I think I'll take a look at the Polar command to see what I can come up with. At least I'm learning a lot figuring all this out. We do purge out regapps on a regular basis. I'm still amazed at the number of people who are unaware of them...

    BlackBox, your code is returning the error "too few arguments", but I can see the examples of what you've done. Thanks for your help. I was wondering about adding error messages and what not.
    Apologies for the error; I was interrupted 5-6 times while trying to write that post, and simply posted a mistake, which has since been corrected in my earlier post.

    In any event, I'm glad you found the comments useful - Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

Similar Threads

  1. 2012: Making Property Set For Duct Object Always On
    By chris4455 in forum AMEP General
    Replies: 0
    Last Post: 2012-11-27, 09:02 PM
  2. Merge MTEXT objects into one MTEXT object
    By autocad.wishlist1734 in forum AutoCAD Wish List
    Replies: 0
    Last Post: 2007-07-24, 02:33 PM
  3. How we get the object property
    By avinash00002002 in forum VBA/COM Interop
    Replies: 6
    Last Post: 2006-04-28, 11:34 AM
  4. Replies: 0
    Last Post: 2005-02-09, 09:08 PM
  5. Property Data Sets - Object Overrides
    By welk3d-cad1 in forum ACA Wish List
    Replies: 0
    Last Post: 2004-12-23, 03:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •