View Full Version : Getting the centroid of closed polyline
csgohjmj
2004-07-08, 11:51 PM
Hi;
I am trying get the coordinates of the centroid of a closed polyline but do not have any idea at all. Is there any lisp routine whereby the coordinate can be obtained? Any help is much appreciated. Thanks.
csgoh
9-7-04
David.Hoole
2004-07-09, 07:35 AM
Hi csgoh
Use the REGION command to convert the Polyline to a Region, then use the MASSPROP command to list its physical properties, which include the centroid.
csgohjmj
2004-07-09, 02:32 PM
Hi David,
Thanks for the tip but using massprop only lists out the properties. But I need to get the centroid coordinates in order to insert text at this point and how can I do this?
Thanks.
csgoh
David.Hoole
2004-07-09, 03:09 PM
Hi csgoh
Sorry, forgot we were in a programming forum! Try this:
(setq obj (vlax-ename->vla-object (car (entsel))))
(setq centroid (vlax-safearray->list (vlax-variant-value (vla-get-centroid obj))))
Hope I got the code window right! Fluffed it last time
David.Hoole
2004-07-09, 03:47 PM
Hi csgoh
Looks like I fluffed the text window again :-( . I may have to read the instructions ;-)
Thought you might find a practical example useful:
(defun c:get_centroid ( / ename obj centroid)
(setq ename (car (entsel "\nSelect polyline:")))
(command "region" ename "")
(setq ename (entlast))
(setq obj (vlax-ename->vla-object ename))
(setq centroid (vlax-safearray->list (vlax-variant-value (vla-get-centroid obj))))
(command "undo" "")
(command "text" "j" "mc" centroid 2.5 0.0 "CENTROID")
(princ)
)
csgohjmj
2004-07-11, 02:56 PM
Dear David;
Thanks but the code that you have given is good for ACAD 2K & above. How about those older version like Autocad R14 version. I have tried using entget to retrieve the centroid but to no avail and I hope you could help me.
Thanks again.
csgoh
11-7-04
csgohjmj
2004-08-30, 01:35 AM
Hi all;
What is a variant? I went through the help section of ACAD R14 and tells if I need to obtain the centroid coordinates, I will have to use <variant>=object.Centroid and define these :-
Dim anObj as object
Dim retCentroid as Variant
retCentroid=anObj.Centroid
Could someone please tell me how to incorporate these codes into Autolisp? Some of our machines are using ACAD R14 version. Any help to get the centroid in ACAD R14 lisp codes is much appreciated.
thanks.
csgoh
stig.madsen
2004-08-30, 09:37 AM
A variant is a variable that can hold one of several different data types. It's used in Visual Basic and has no direct counterpart in AutoLISP but can be read and assigned by functions in Visual LISP.
If I remember correctly, R14 does not have access to ActiveX or VBA - even with the add-on Visual LISP for R14c, I think (could be very wrong on this) - so you may have to get a function that does all the analyses and math itself. Did you search the AUGI Exchange?Otherwise, searching for "centroid AutoLISP" in Google turned up 238 hits so maybe there is hope.
jwanstaett
2004-08-30, 02:20 PM
A variant is a variable that can hold one of several different data types. It's used in Visual Basic and has no direct counterpart in AutoLISP but can be read and assigned by functions in Visual LISP.
If I remember correctly, R14 does not have access to ActiveX or VBA - even with the add-on Visual LISP for R14c, I think (could be very wrong on this) - so you may have to get a function that does all the analyses and math itself. Did you search the AUGI Exchange?Otherwise, searching for "centroid AutoLISP" in Google turned up 238 hits so maybe there is hope.
no r14 was when Autodesk added VBA to AutoCad
stig.madsen
2004-08-30, 02:38 PM
Well, I must be getting old. So csgohjmj should have no problem running the code David provided on R14?
csgohjmj
2004-08-31, 07:23 AM
Hi;
I have tried David's code on ACAD R14 and 2002 by entering the code at command line.
However it gives me an [error: null function in ACAD R14 and no function definition in ACAD 2002] when I key in these code [ (setq obj(vlax-ename->vla-object ename)) ].
Now what have I done wrong here? Is there anything else that I have to do in order to be able to use the vl/vla funtions? This is my first attempt to use the vl functions in autolisp and I hope to get some guidance on these.
Thanks.
csgoh
Mike.Perry
2004-08-31, 07:47 AM
Hi
Try adding -
(vl-load-com)
at the beginning of David's routine ie
(vl-load-com)
(defun c:get_centroid ( / ename obj centroid)
....
Have a good one, Mike
csgohjmj
2004-08-31, 08:13 AM
Thanks Mike but it does not work in ACAD R14. Guess ACAD R14 does not support vl functions. Seems that I have to save the massprop into a file and retrieve the centroid - a longer process as compared to ACAD 2002.
Thanks.
csgoh
Mike.Perry
2004-08-31, 10:34 AM
Hi
For a little Visual LISP history regarding AutoCAD R14 check out the following links -
Visual LISP For AutoCAD R14 (http://www.aecnews.com/online/Back_Issues/1998/aec1998-05/03-sw_dev.html)
AutoCAD Customization Overview (http://cadence.advanstar.com/2002/0602/vpoint0602.html)
History of AutoLISP (http://courses.home.att.net/autolisp/ahistory.htm)
Have a good one, Mike
kennet.sjoberg
2004-09-07, 03:22 PM
Hi csgohjmj ! ?
Here is my old working R14 stuff.
a point is created in the centroid, check the pdmode, DDptype.
If you want Moment, Products, Radii from the centroid, You can run
TP again in that point "Select point for centroid calculation ".
(defun c:tp(/ ucsic ents osm )
;;; 2000-05-19 kennet
(defun calc (/ org tpfil I Xrad Yrad Ytp Ytp TP )
(setq org (getpoint "Select point for centroid calculation "))
(command "._ucs" "Origin" org )
(setvar "UCSICON" 3 )
(command "._massprop" "l" "" "Y" "C:$$temp$$")
(command "._erase" "l" "")
(setq tpfil (open "C:$$temp$$.mpr" "r"))
(setq I 0 )
(while (< I 8 )
(setq Xrad (read-line tpfil ))
(setq I (1+ I))
)
(setq Yrad (read-line tpfil ))
(close tpfil )
(command "._del" "C:$$temp$$.mpr")
(setq Xtp (float(atof (substr Xrad 26 10 ))))
(setq Ytp (float(atof (substr Yrad 26 10 ))))
(setvar "OSMODE" 0 )
(command "._point" (list Xtp Ytp) )
(setvar "OSMODE" osm )
(setvar "UCSICON" ucsic )
(command "._ucs" "p")
(textscr)
)
(defun not_ok ()
(command "._erase" "p" "")
(princ "!! Cant make a closed are !! ")
)
(setvar "CMDECHO" 0 )
(setq ucsic (getvar "UCSICON"))
(setq osm (getvar "OSMODE"))
(prompt "nSelect objects in a closed area : ")
(setq ents (ssget))
(command "._copy" "p" "" "0,0,0" "0,0,0" )
(command "._region" "p" "")
(if (= (cdr (assoc 0 (entget (entlast)))) "REGION" ) (calc) (not_ok) )
(princ)
)
Happy Computing !
kennet
csgohjmj
2004-10-09, 01:50 PM
Hi David;
Thanks for the lisp code for getting the centroid. However, I encounter an error ;saying that it is a self intersecting loop. There is nothing wrong in your code at all. I have even used the command "region" and select a closed polyline and it gives me the same error messages. I also have attached the dwg file for this discusion as to how to solve the problem. The polygon in the dwg looks like it is self intersecting, but it you zoom close enough, it is not so. It so happen that the lines on the right side of the polygon differs by only 0.0001mm. Or is there anyother way ? Pls help.
Thanks
csgoh
csgohjmj
2004-10-09, 02:31 PM
Forgot about the dwg attachment. Here is a sample of polygon.
kennet.sjoberg
2004-10-11, 05:52 AM
Hi Donald ; )
Here is a solution for you :
Scale the polygon 1000 times, run TP, scale back again 0.001 times
(scale in the same point, take the tp-point with you )
Happy Computing !
My name is still kennet
csgohjmj
2004-10-11, 12:55 PM
Hi Kennet;
In fact, tried out your suggestion and it works. Anyway, the problem I am actually facing is the command "region" even when I type it at command prompt without any lisp program. I tested in acad2002 in xp pro service pack and acad14 and both give me an error but not acad2004 & 2005. Why is that?
Thanks
csgoh
p/s- Kennet, my last post was actually directed to David from the earlier reply in these post.
kennet.sjoberg
2004-10-11, 01:29 PM
OK, but Davids code was not for R14. . .
Probably AutoCAD do better calculating nowadays ; )
Happy Computing !
kennet
David.Hoole
2004-10-12, 08:36 AM
Hi csgoh
Sorry, but I can't think of a solution other than Kennet's.
I don't know what these polylines represent or how they are being produced, but it seems to me that such a small dimension suggests a slight misalignment in the drawing rather than a real area/object. Please don't take this as a criticism, just trying to look at the problem practically.
csgohjmj
2004-10-12, 10:09 PM
Hi David;
The issue here is not how the polylines are created or what it is for. The issue here is why it gives different results in acad14 & 2002 but not acad2004 & 2005 even it is the same polyline.
csgoh
kennet.sjoberg
2004-10-13, 05:51 AM
Once again, "Probably AutoCAD do better calculating nowadays ; )"
to scale objects up and down is one solution for you, sometimes it is enough to move the objects closer to 0,0,0 especially when you are working in 3D with solids.
the phenomenon can appear when you create hath in R14 or R2002, and there do R2002 better calculation.
:. small and large numbers make the sense and miscalculation, the thing you can do is to avoid them.
Happy Computing !
kennet
csgohjmj
2004-10-14, 11:40 PM
Kennet,
Thanks for your explanation and I will take heed of your advice in the future.
csgoh
kennet.sjoberg
2004-10-15, 06:01 AM
OK csgoh glad if I can help, hope you enjoy the R14 centroid stuff.
: ) Happy Computing !
kennet
By the way the uploaded region is a typical example that will cause trouble to AutoCAD,
the best thing there is to avoid the unnecessary peripheral node from the beginning.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.