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)