Author Topic: Field Expression (a simple formula or is it?)  (Read 2525 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Swamp Rat
  • Posts: 812
Field Expression (a simple formula or is it?)
« on: October 16, 2017, 04:45:37 PM »
Hey guys,
I'm trying to figure out how to create a field like this:

Code: [Select]
(/ TotalFieldAreaFromSS1 TotalFieldAreaFromSS2)
Basically prompt for two selection sets, i.e. 'SS1' and 'SS2' then for each Selection Set:
I guess I should construct the total area list or maybe the overall total area field expression?
So the task I couldn't figure out is to divide their total area fields with each other, so I could end up with a relative factor which is a field aswell.

I've dissected Lee's total area field program a.k.a. Areas2Field and easily found how to construct the total area field expression [thanks Lee!],
but like I said the trouble I have is when I have to implement a small formulas like:
Code: [Select]
(/ TotalArea1 TotalArea2)and
Code: [Select]
(* (/ TotalArea1 TotalArea2) 100)
So any idea how that field expression should be constructed?



This is a fragment from Lee's code that I mentioned about:

Code - Auto/Visual Lisp: [Select]
  1. ; Fragment from Lee Mac's Areas2FieldV-13.lsp code
  2. (setq sel (ssget '((0 . "ARC,CIRCLE,ELLIPSE,HATCH,*POLYLINE,REGION,SPLINE"))))
  3. (setq fmt "%lu6%qf1") ;; Field Formatting
  4.  
  5. (if (= 1 (sslength sel))
  6.   (setq str
  7.     (strcat
  8.       "%<\\AcObjProp Object(%<\\_ObjId "
  9.       (LM:ObjectID (vlax-ename->vla-object (ssname sel 0)))
  10.       ">%).Area \\f \"" fmt "\">%"
  11.     )
  12.   )
  13.   (progn
  14.     (repeat (setq idx (sslength sel))
  15.       (setq lst
  16.         (vl-list*
  17.           "%<\\AcObjProp Object(%<\\_ObjId "
  18.           (LM:ObjectID (vlax-ename->vla-object (ssname sel (setq idx (1- idx)))))
  19.           ">%).Area>%" " + "
  20.           lst
  21.         )
  22.       )
  23.     )
  24.     (setq str
  25.       (strcat
  26.         "%<\\AcExpr "
  27.         (apply 'strcat (reverse (cdr (reverse lst))))
  28.         " \\f \"" fmt "\">%"
  29.       )
  30.     )
  31.   )
  32. )


Maybe I'll end up with a subfunction like:
Code: [Select]
(foo SS1 SS2 fmt1 fmt2)
Which would return:
Code: [Select]
(list TotalAreaSS1_fmt1 TotalAreaSS2_fmt1 RelFactor_fmt2)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Field Expression (a simple formula or is it?)
« Reply #1 on: October 16, 2017, 06:05:46 PM »
Quick test .. something like this works:  (1.455730+1.455730+1.455730)/(1.455730+1.455730+1.455730)

Quote
%<\AcExpr ((%<\_FldPtr 2140757529952>%+%<\_FldPtr 2140757529152>%+%<\_FldPtr 2140757530176>%)/(%<\_FldPtr 2140757530336>%+%<\_FldPtr 2140757529440>%+%<\_FldPtr 2140757531264>%))>%

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Field Expression (a simple formula or is it?)
« Reply #2 on: October 17, 2017, 05:16:00 AM »
Thanks alot Ron, I did a brief testing and it worked:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun DivideTwoTotalFieldsExpr ( SS1 SS2 fmt1 fmt2 / sum1 sum2 r )
  3.   (if (vl-every 'set '(sum1 sum2) (mapcar 'SS->FieldSum (list SS1 SS2)))
  4.     (setq r (strcat "%<\\AcExpr " "(" sum1 " / " sum2 ")" " \\f \"" fmt2 "\">%"))
  5.   )
  6. ); defun
  7.  
  8. (defun SS->FieldSum ( SS / fmt i o L r )
  9.  
  10.   ; (setq fmt "%lu6%qf1")
  11.   (cond
  12.     ( (not (eq 'PICKSET (type SS))) )
  13.     (
  14.       (progn
  15.         (repeat (setq i (sslength SS))
  16.           (and
  17.             (setq o (vlax-ename->vla-object (ssname SS (setq i (1- i)))))
  18.             (vlax-property-available-p o 'Area)
  19.             (setq L (cons (strcat "%<\\AcObjProp Object(%<\\_ObjId " (LM:ObjectID o) ">%).Area>%") L))
  20.           ); and
  21.         ); repeat
  22.         (not L)
  23.       ); progn
  24.     )
  25.     ( (= 1 (length L))
  26.       ; (setq r (car L)) ; Test #1 ; WORKS
  27.       (setq r (strcat "%<\\AcObjProp Object(%<\\_ObjId " (LM:ObjectID o) ">%).Area \\f \"" (cond (fmt) ("")) "\">%")) ; Test #2 ; WORKS
  28.     )
  29.     ( (setq r (apply 'strcat (mapcar (function (lambda (x) (strcat x " + "))) (cdr L))))
  30.       (setq r (strcat r (car L)))
  31.       ; (setq r (strcat "(" r ")")) ; Test #1 ; WORKS
  32.       (setq r (strcat "%<\\AcExpr " r " \\f \"" (cond (fmt) ("")) "\">%")) ; Test #2 ; WORKS
  33.     )
  34.   ); cond
  35.   r
  36. ); defun
  37.  
  38.  
  39.  
  40. ;; ObjectID  -  Lee Mac
  41. ;; Returns a string containing the ObjectID of a supplied VLA-Object
  42. ;; Compatible with 32-bit & 64-bit systems
  43.  
  44. (defun LM:ObjectID ( obj )
  45.   (eval
  46.     (list 'defun 'LM:ObjectID '( obj )
  47.       (if
  48.         (and
  49.           (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
  50.           (vlax-method-applicable-p (vla-get-utility (LM:acdoc)) 'getobjectidstring)
  51.         )
  52.         (list 'vla-getobjectidstring (vla-get-utility (LM:acdoc)) 'obj ':vlax-false)
  53.         '(itoa (vla-get-objectid obj))
  54.       )
  55.     )
  56.   )
  57.   (LM:ObjectID obj)
  58. )

Code - Auto/Visual Lisp: [Select]
  1. (
  2.   (lambda ( / SS1 SS2 s p )
  3.     (and
  4.       (princ "\nFirst selection: ") (setq SS1 (ssget "_:L"))
  5.       (princ "\nSecond selection: ") (setq SS2 (ssget "_:L"))
  6.       (setq s (DivideTwoTotalFieldsExpr SS1 SS2 nil "%lu2%qf1"))
  7.       (setq p (getpoint "\nPlace the field: "))
  8.     )
  9.   )
  10. )

It seems wherever you use
Code: [Select]
((AreaField1 + AreaField2 + AreaField3 + ...) / (AreaField1 + AreaField2 + AreaField3 + ...))or
Code: [Select]
(TotalAreaFieldExpr / TotalAreaFieldExpr)it would work.

Looks simplier than I've thought.. since I was confused with the LISP/DIESEL expressions..
Code: [Select]
(/ (+ a1 a2 a3 ...) (+ a1 a2 a3 ...))
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

ronjonp

  • Needs a day job
  • Posts: 7528
Re: Field Expression (a simple formula or is it?)
« Reply #3 on: October 17, 2017, 10:02:44 AM »
Glad you got it sorted.  :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Field Expression (a simple formula or is it?)
« Reply #4 on: October 17, 2017, 01:17:54 PM »
Glad you got it sorted.  :)

All I needed was a little push-up, like yours! :)

The code is still a bit messy, but its still a good start when I find free time to develop it.
I guess this thread would help one when dealing with field formulas in LISP.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg