Jump to content

Centre of Gravity of a group of Open or Closed Polylines

Recommended Posts


I work as a boat designer and often require calculating total length of polyline & centre of gravity (both open & closed polylines).

I have a snippet below to show the sample code I am currently using - this only works with a group of lines - it doesnt work with polylines.

Currently everytime I have to calculate the CG, I have to copy the polyline over, explode it & select the lines to calculate the CG.


The logic is as follows - assuming we have 3 segments on the polyline(A,B,C) :

Centre of Gravity of Polyline = (Len of A multiplied by Centroid of A + Len of b multiplied by Centroid of B + Len of C multiplied by Centroid of C) divided by Total Length of polyline


I am new to LISP & although have some background in other programming languages, find it is relatively difficult to write the code, the syntax being very different.

I found a similar code ( TotalADD Total Addition by Andrea Andreetti & modified to come to the below code).


It would be ideal if I can select a group of polylines (using select similar) and run the command to get a text table in autocad with CG & total length calculated for set of polylines on different layers.

But I am not sure how this can be achieved. Would you be able to help or am I asking for too much ? any guidance will be much appreciated.



	      (if (eq (vla-get-objectname n) "AcDbLine")
		(setq itemlinelength (+ itemlinelength (vla-get-length n)))
		(setq StartPoint(vlax-safearray->list
							(vlax-variant-value (vla-get-startpoint n))))
		(setq x1 (car Startpoint))
		(setq y1 (cadr Startpoint))

		(setq EndPoint(vlax-safearray->list
							(vlax-variant-value (vla-get-endpoint n))))
		(setq x2 (car endpoint))
		(setq y2 (cadr endpoint))
		(setq xmid (* (+ x1 x2) 0.5))
		(setq ymid (* (+ y1 y2) 0.5))
		(setq xmom (* xmid (vla-get-length n)))
		(setq ymom (* ymid (vla-get-length n)))
		(setq itemxmoment (+ itemxmoment xmom))
		(setq itemymoment (+ itemymoment ymom))
(setq totxcg (/ itemxmoment itemlinelength))
(setq totycg (/ itemymoment itemlinelength))
(setq tlength (rtos itemlength 2 )
(setq totxcg (rtos totxcg 2 )
(setq totycg (rtos totycg 2 )
(acet-ui-status (strcat "Length:     " tlength "\n" 
					"X CoG Pos:   " totxcg "\n" "Y CoG Pos:    " totycg "\n"

Share this post

Link to post
Share on other sites

Welcome to CADTutor.


Coming from another programming language to LISP can be a different world, but it is ultimately just a different Object Model, with its own Objects, Properties, Methods, and Events - a vocabulary, so to speak.


To simplify your programmatic effort, you can process your original Polyline(s), using some Visual LISP (ActiveX) functions, specifically the vlax-Curve* functions in order to cull the necessary coordinates of said Polyline's vertexes. That list of coordinates can be evaluated to obtain the CG.


If you wanted to save some time, and not have to develop this yourself, you could always download Gile's Custom OSNAP app (a C# plugin released back in +/- 2009?), which implements a new CTR (Centroid) OSNAP. If you cannot find his publicly posted source code, and compile yourself, he also sells an Autodesk Exchange App - both of which provide much more than this one feature.


Also worthy of note, is that AutoCAD 2016 has finally added this as a new feature, by way of a new GCE (Geometric Center) OSNAP - something I affectionately refer to as the Gilles Chanteau Emulator, as it is similar, but falls just short of the original. Haha



Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...