See the top rated post in this thread. Click here

Results 1 to 4 of 4

Thread: Field String Create Example

  1. #1
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,122
    Login to Give a bone
    1

    Default Field String Create Example

    I had need to study creating a field string.

    I found Lee's page, but the code seemed over complicated ...

    Here is the link:

    https://lee-mac.com/quickfield.html

    So I simplified the code into my style.

    I thought you all might like to have this in your bag of tricks.

    If you look at the example at the bottom you can create a field string for the length of a line lunits 4 precision 0.

    P=

    Code:
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to create a field string 
    ;___________________________________________________________________________________________________________|
    
    (defun FieldStringCreate (objItem strProperty strFormat / objUtility strObjectID)
     (if (and (vlax-Property-Available-P objItem strProperty)
              (setq objUtility  (vla-get-utility (vla-get-activedocument (vlax-get-acad-object))))
              (setq strObjectID (vla-GetObjectidString objUtility objItem :vlax-false))
         )
      (strcat "%<\\AcObjProp Object(%<\\_ObjId "
              strObjectID
              ">%)."
              strProperty
              (if (= strFormat "")
               ">%"
               (strcat " \\f \"" strFormat "\">%")
              )    
      )
     )
    )
    
    ; Example for length of line with lunits 4 and precision 0: 
    ; (FieldStringCreate objLine "length" "%lu4%pr0")
    
    (princ "!")
    (vl-load-com)
    Attached Files Attached Files
    AutomateCAD

  2. #2
    All AUGI, all the time
    Join Date
    2003-07
    Posts
    565
    Login to Give a bone
    2

    Default Re: Field String Create Example

    Hi Peter In some other forums very recently is add prefix and suffix, pre is %%c suffix is m, this was for reo bar, two fields, the length and the width.

    You are right though making the field codes work can be very complicated when you start combining fields.

    An example is a block 3 attributes, the 3rd attribute is the sum of the 1st and 2nd attributes. Another like this a rectang length & area. The reason I mention it is that with blocks using attributes you have to redo the ID for each block attribute individually something that catches a lot of people out.

    Code:
    ; add 2 attributes and put the value as a field into the 3rd attribute.
    ; BY AlanH
    
    (defun c:test ( / obj lst x )
    (setq oldatt (getvar 'attdia))
    (setvar 'attdia 0)
    (command "-insert" "c" (getpoint "\npick point") 1 1 0 (getstring "\nEnter Att1 ") (getstring "\nEnter Att2 ") (getstring "\nEnter Att3 ") "-")
    (setq obj (vlax-ename->vla-object (entlast)))
    (setq lst '())
    (foreach att (vlax-invoke obj 'getattributes)
    (princ  "\n")
    (setq lst (cons  (strcat "%<\\AcObjProp Object(%<\\_ObjId " 
    (vlax-invoke-method (vla-get-Utility  (vla-get-activedocument (vlax-get-acad-object))) 'GetObjectIdString att :vlax-false)
    ">%).Textstring>%"
     ) lst ))
    )
    (setq str nil)
    (setq x (length lst))
    (setq str (strcat "%<\\AcExpr "
    (nth (setq  x (- x 1)) lst) " + "
    (nth (setq  x (- x 1)) lst) " + "
    (nth (setq  x (- x 1)) lst) ">%"
    )
    )
    (setq x 1 y 4)
    (foreach att(vlax-invoke obj 'getattributes)
    (if (= x y)
    (Vla-put-textstring att str)
    )
    (setq x (+ x 1))
    )
    (setvar 'attdia oldatt)
    (princ)
    )
    (c:test)
    Almost forgot when you use field with mtext you can copy the result a quick cheat, but you need to make sure in lisp that you use \\ not a single backslash and use \ " for quotes
    Last edited by BIG-AL; 2024-09-23 at 07:07 AM.

  3. #3
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,122
    Login to Give a bone
    1

    Default Re: Field String Create Example

    Big Al,

    I liked your example.

    I generalized it into my style and allowed for other operands and formats.

    P=

    Code:
    ;___________________________________________________________________________________________________________
    ;
    ; Written By: Peter Jamtgaard C.E., P.E., S.E. copyright 2024 All Rights Reserved
    ;___________________________________________________________________________________________________________
    ;
    ; Abstract: Example of creating an field equation using attribute values
    ;___________________________________________________________________________________________________________|
    ;
    ; Command Line Function Header List
    ;___________________________________________________________________________________________________________|
    
    ;* C:AEQ
    ;* Command Line Function to create an equation for attribute values and place result field in last attribute
    
    ;* C:AttributeEquation
    ;* Command Line Function to create an equation for attribute values and place result field in last attribute
    ;___________________________________________________________________________________________________________|
    ;
    ; General Function Header List
    ;___________________________________________________________________________________________________________|
    ;
    ;  Function List		Argument1	Argument2 	Arguement3
    
    ;* (AttributeEquation objBlock strOperand strFormat)
    ;* Function to create an equation for attribute values and place result field in last attribute
    
    ;* (FieldStringCreate objItem strProperty strFormat)
    ;* Function to create a field string for an object using property and format
    
    ;* (FieldStringsFormula lstFieldStrings strOperand strFormat )
    ;* Function to create a formula of fields with operands (+,-,*,/) and format
    
    ;$ End Header
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Command Line Function to create an equation for attribute values and place result field in last attribute
    ;___________________________________________________________________________________________________________|
    
    (defun C:AEQ ()(C:AttributeEquation))
    
    (defun C:AttributeEquation (/ entSelection objSelection ssSelections)
     (if (and (princ "\nSelect Block with 3 Attributes: ")
              (setq ssSelections (ssget ":S:E" (list (cons 0 "INSERT")(cons 66 1))))
              (setq entSelection (ssname ssSelections 0))
              (setq objSelection (vlax-ename->vla-object entSelection))
         )
      (AttributeEquation objSelection "+" "%lu2%pr2")
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to create an equation for attribute values and place result field in last attribute
    ;___________________________________________________________________________________________________________|
    
    (defun AttributeEquation (objBlock 
                              strOperand 
                              strFormat / 
                              lstAttributes 
                              lstFieldStrings 
                              objAttributeResult 
                              objDocument
                              strFieldStringSum
                             )
     (if (and (setq lstAttributes      (vlax-invoke objBlock "getattributes"))
              (> (length lstAttributes) 2)
              (setq objAttributeResult (car (reverse lstAttributes))) ; Last attribute
              (setq lstAttributes      (vl-remove objAttributeResult lstAttributes))
              (setq lstFieldStrings    (mapcar '(lambda (X)(FieldStringCreate X "textstring" "")) lstAttributes))
              (setq strFieldStringSum  (FieldStringsFormula lstFieldStrings strOperand strFormat))
         )
      (vla-put-textstring objAttributeResult strFieldStringSum)
      (alert "Error executing Attribute Equation Function: ")
     )
     (vl-cmdf "regenall")
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to create a field string for an object using property and format
    ;___________________________________________________________________________________________________________|
    
    (defun FieldStringCreate (objItem strProperty strFormat / objUtility strObjectID)
     (if (and (vlax-Property-Available-P objItem strProperty)
              (setq objUtility  (vla-get-utility (vla-get-activedocument (vlax-get-acad-object))))
              (setq strObjectID (vla-GetObjectidString objUtility objItem :vlax-false))
         )
      (strcat "%<\\AcObjProp Object(%<\\_ObjId "
              strObjectID
              ">%)."
              strProperty
              (if (= strFormat "")
               ">%"
               (strcat " \\f \"" strFormat "\">%")
              )    
      )
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to create a formula of fields with operands (+,-,*,/) and format
    ;___________________________________________________________________________________________________________|
    
    (defun FieldStringsFormula (lstFieldStrings strOperand strFormat / strFieldString strFieldStringReturn)
     (foreach strFieldString lstFieldStrings
      (if strFieldStringReturn
       (setq strFieldStringReturn (strcat strFieldStringReturn " " strOperand " " strFieldString))
       (setq strFieldStringReturn (strcat "%<\\AcExpr " strFieldString))
      )
     ) 
     (if (= strFormat "")
      (strcat strFieldStringReturn ">%")
      (strcat strFieldStringReturn " \\f \"" strFormat "\">%")
     ) 
    )
    
    (princ "!")
    (vl-load-com)
    Attached Files Attached Files
    AutomateCAD

  4. #4
    All AUGI, all the time
    Join Date
    2003-07
    Posts
    565
    Login to Give a bone
    1

    Default Re: Field String Create Example

    Glad to have helped.

Similar Threads

  1. String Parameters & Field Functionality in a Single Block
    By Wish List System in forum AutoCAD Wish List
    Replies: 0
    Last Post: 2014-08-18, 04:45 PM
  2. how to replace only a single string by pre decided string.
    By mohammad.raees637333 in forum AutoLISP
    Replies: 3
    Last Post: 2014-04-22, 01:07 PM
  3. Convert Field Value to String?
    By stusic in forum AutoLISP
    Replies: 1
    Last Post: 2012-04-04, 04:03 PM
  4. Field in text string?
    By jsteinhauer in forum Revit Architecture - General
    Replies: 3
    Last Post: 2011-11-15, 02:43 PM
  5. Can I view the SQL string generated by Insert>Field?
    By everpat in forum VBA/COM Interop
    Replies: 6
    Last Post: 2006-10-10, 02:05 AM

Posting Permissions

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