Lisp for calculating geometric properties

Lisp for calculating geometric properties

rafael.bizarro
Participant Participant
2,997 Views
27 Replies
Message 1 of 28

Lisp for calculating geometric properties

rafael.bizarro
Participant
Participant

Good afternoon my friends. I really need a help to elaborate this LISP according to the step-by-step in the image below. I would be very grateful for your help. Thank you in advance.

image.png

0 Likes
Accepted solutions (2)
2,998 Views
27 Replies
Replies (27)
Message 2 of 28

rafael.bizarro
Participant
Participant

If it's too complicated with hollow figures. It might just be with massive figures. (like the 1st example)

0 Likes
Message 3 of 28

Sea-Haven
Mentor
Mentor
Accepted solution

A quicky, use bpoly then (setq pt (osnap (vlax-curve-getStartPoint (vlax-ename->vla-object (entlast))) "gcen")) this is geometric centre not sure if centre of gravity will have to check. Then erase last, and make a new one add answer to table. So long as shape is one bpoly. A double "I" touching will give wrong answer, must have the sections removed that touches like image.

 

Post a dwg with a couple of shapes and a table.

0 Likes
Message 4 of 28

hak_vz
Advisor
Advisor

@rafael.bizarro  When posting request to this forum always attach sample drawing. This helps us to create your code without too much fuss. What we have in jpeg my not represent fully how your drawing  is created i.e using lines polylines, different measuring units... you name it.

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 5 of 28

rafael.bizarro
Participant
Participant

Ok! Ty for the tip! Here it is the dwg.

0 Likes
Message 6 of 28

Kent1Cooper
Consultant
Consultant
Accepted solution

Try this as a start:

 

(defun C:WHATEVER (/ *error* perim file minpt maxpt LL UR pickpt cut inc ent outer inner outerobj)
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg))
    ); if
    (close file)
    (princ)
  ); defun - *error*
  (setq
    perim (car (entsel "\nSelect outline [outer perimeter if hollow]: "))
    file (open "C:/Your/File/Path/YourFileName.csv" "w"); <-------EDIT filepath and name
  ); setq
  (write-line "Step,Area,COG depth,Product" file); headers  <-------EDIT wordings
  (vla-getboundingbox (vlax-ename->vla-object perim) 'minpt 'maxpt)
  (setq
    LL (vlax-safearray->list minpt)
    UR (vlax-safearray->list maxpt)
    pickpt (list (/ (+ (car LL) (car UR)) 2) (- (cadr UR) 0.5)); 1/2 unit below top middle
  ); setq
  (command
    "_.zoom" "_object" perim ""
    "_.xline" "_hor" "_non" UR ""
  ); command
  (setq
    cut (entlast); the Xline
    inc 0
  ); setq
  (while (> (caddr (assoc 10 (entget cut))) (cadr LL)); still intersects
    (command
      "_.move" cut "" "0,-1" ""
      "_.boundary" "_advanced" "_object" "_region" "" pickpt ""
    ); command
    (setq
      inc (1+ inc)
      ent cut
      outer (setq ent (entnext ent)); the outermost Region [first if multiple]
      inner (ssadd); initially empty selection set of inner Region(s)
    ); setq
    (while (setq ent (entnext ent)); internal Region(s)?
      (ssadd ent inner); put in selection set
    ); while
    (if (> (sslength inner) 0); were there internal Regions?
      (command "_.subtract" outer "" inner ""); outer keeps entity name
    ); if
    (setq outerobj (vlax-ename->vla-object outer))
    (write-line
      (strcat ;   <-------EDIT precision arguments in three (rtos) functions
        (itoa inc) "," ; Line number
        (rtos (setq area (vla-get-Area outerobj)) 2 3) "," ; Area
        (rtos (setq cogd (- (cadr UR) (cadr (vlax-get outerobj 'Centroid)))) 2 3) "," ; COG depth
        (rtos (* area cogd) 2 3)
      ); strcat
      file
    ); write-line
    (entdel outer)
  ); while [move Xline]
  (entdel cut)
  (close file)
  (princ)
); defun

 

EDIT the command name from "WHATEVER" to whatever you prefer, and see the lines marked  <-------EDIT  for other things you need to change to suit your setup and preferences.

 

It does not yet do the Table in the drawing, but it does the spreadsheet file, since I know more readily how to do that.  It's not difficult to make a Table and just link the .csv file -- I haven't looked into automating that.

 

It should work for shapes such as in your original image, but there could be configurations in which it would fail, particularly these possibilities:

A)  if the location at 1/2 unit below the top at the middle of the width is outside the perimeter object; or

B)  if anything crosses that location, so that the pick for the BOUNDARY command will be exactly on something.

 

Also, it relies on something I think is always true, but I'm not sure:  that if BOUNDARY results in more than one Region, the outermost of them is always the first one in drawing order.

 

But give it a try, for what it can do so far.

Kent Cooper, AIA
0 Likes
Message 7 of 28

rafael.bizarro
Participant
Participant

Thank you first of all for the effort! I am trying to use the routine but this error is resulting.

rafaelbizarro_0-1631198103142.png

 

0 Likes
Message 8 of 28

Kent1Cooper
Consultant
Consultant

In AutoLisp, file path separators need to be either double backslashes or single forward slashes:

....

  file (open "C:/Users/Rafael/OneDrive/Backup Scalata/Arquivo/Utilidades/Lisps autocad/teste areacomprimida.csv" "w")

....

Kent Cooper, AIA
0 Likes
Message 9 of 28

rafael.bizarro
Participant
Participant

Now it worked but if the figure is in meters, it calculates only 2 points. I tried to put a figure in centimeters but it did almost 90,000 calculations.

0 Likes
Message 10 of 28

Kent1Cooper
Consultant
Consultant

It was written to work in plain drawing units.  Since everything in the original image was in terms of centimeters, I assumed the drawing unit is a centimeter.  If your drawing unit is really a meter, but you want things reported in centimeter terms, then some lines need to be changed:

 

      "_.move" cut "" "0,-0.01" ""

 

and

 

        (rtos (setq area (* (vla-get-Area outerobj) 1e4)) 2 3) "," ; Area
        (rtos (setq cogd (* (- (cadr UR) (cadr (vlax-get outerobj 'Centroid)) ) 100)) 2 3) "," ; COG depth

 

I hope that's all that needs to be changed [untested].

Kent Cooper, AIA
0 Likes
Message 11 of 28

rafael.bizarro
Participant
Participant

I made the changes but it's still doing a lot of calculations. More than 30,000 lines in excel. Here are the files I'm using.

0 Likes
Message 12 of 28

Kent1Cooper
Consultant
Consultant

Just to confirm:  That T shape is 2 meters [2 drawing units] wide and 1.95 meters tall?  And you want it checked at centimeter intervals?  That's 195 slices.

Kent Cooper, AIA
0 Likes
Message 13 of 28

Kent1Cooper
Consultant
Consultant

Something I missed before -- correct this line from 0.5 to 0.005:

    pickpt (list (/ (+ (car LL) (car UR)) 2) (- (cadr UR) 0.005)); 1/2 cm below top middle

so that the pick point for the BOUNDARY commands is between the top and the first slice level.  But I do wonder, with the Zoom level out where you see the whole shape, and 195 slices, whether that pick point is going to be seen correctly, being extremely close to the top edge.

Kent Cooper, AIA
0 Likes
Message 14 of 28

rafael.bizarro
Participant
Participant

Yes, correct

0 Likes
Message 15 of 28

Kent1Cooper
Consultant
Consultant

Try running it with Object Snap off.

Kent Cooper, AIA
0 Likes
Message 16 of 28

rafael.bizarro
Participant
Participant

You can set the first pick point to 5 centimeters from the top if helps

0 Likes
Message 17 of 28

Kent1Cooper
Consultant
Consultant

@rafael.bizarro wrote:

You can set the first pick point to 5 centimeters from the top if helps


You mean the first slice level 5cm down?  But then at 1cm intervals continuing down?

Kent Cooper, AIA
0 Likes
Message 18 of 28

rafael.bizarro
Participant
Participant

You mean the first slice level 5cm down?  But then at 1cm intervals continuing down?

Yes.
 
But i Ran without snap and worked fine.
 
0 Likes
Message 19 of 28

rafael.bizarro
Participant
Participant

Now i m having some issues with "." and "," from the data on the excel.

rafaelbizarro_0-1631223343897.png

Some values "." is representing 000 separator and others "." represent ",".

 

0 Likes
Message 20 of 28

Kent1Cooper
Consultant
Consultant

Is there a setting in AutoCAD to report numbers without any thousands separators?

Kent Cooper, AIA
0 Likes