Point at Centroid of a Region

Point at Centroid of a Region

BillAllenSE
Collaborator Collaborator
1,876 Views
16 Replies
Message 1 of 17

Point at Centroid of a Region

BillAllenSE
Collaborator
Collaborator

I don't know anything about VBA or autolisp or even macros in ACAD, but I could use something that would place a point at the centroid of a region. Currently, I use the MASSPROP command, and place a point at the coordinates of the centroid. How can I automate this? Can I generate a macro to do this? I'm sure it would be helpful if the centroid coordinates were properties of the region.
TIA

0 Likes
1,877 Views
16 Replies
Replies (16)
Message 2 of 17

komondormrex
Mentor
Mentor

try this out

 

(defun c:point_centroid (/ region_sset)
	(if (setq region_sset (ssget '((0 . "region"))))
		(foreach region (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex region_sset))))
			(vla-addpoint (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
						  (vlax-3d-point (vlax-get region 'centroid))
			)
		)
	)
	(princ)
)

 

0 Likes
Message 3 of 17

Kent1Cooper
Consultant
Consultant

 

If one at a time is acceptable for you, it simplifies things:

(defun C:PRC (); = Point at Region Centroid
  (command "_.point" (vlax-get (vlax-ename->vla-object (car (entsel))) 'Centroid))
  (prin1)
)

 

Kent Cooper, AIA
0 Likes
Message 4 of 17

_gile
Consultant
Consultant

This one should also work  with closed polylines, closed and planar splines, ...

(command "_.point" "_gcen" pause)

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 17

BillAllenSE
Collaborator
Collaborator

Thanks.

 

I'm so dumb, I don't know what to do with this. Do I create a "button"? Do I need to select the region first? I've never done any lisp programming nor customize my acad environment to run a routine. Pointing me to a place where I can learn would be acceptable if telling me how is too difficult.

TIA

0 Likes
Message 6 of 17

BillAllenSE
Collaborator
Collaborator

Thanks, but I don't know what to do with this. I tried pasting into my command line both before and after selecting a region, but it appears I need to do more than that.

TIA

0 Likes
Message 7 of 17

_gile
Consultant
Consultant

@BillAllenSE wrote:

Thanks, but I don't know what to do with this. I tried pasting into my command line both before and after selecting a region, but it appears I need to do more than that.

TIA


Sorry, my mistake, the '_gcen' osnap does not work with regions.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 17

Kent1Cooper
Consultant
Consultant

@BillAllenSE wrote:

... I don't know what to do with this. ...


First, Ctrl+C Copy the code to the clipboard, then Ctrl+V Paste it in at the command line in AutoCAD, to define the command.  [If it's ending with the final right parenthesis without going back to the Command: prompt, hit Enter to conclude it.]  Then type in the command name PRC, and it will ask you to select the Region and will place the Point object.

 

If that works, we can talk about how to make it more directly available.

Kent Cooper, AIA
0 Likes
Message 9 of 17

BillAllenSE
Collaborator
Collaborator

I can work with polylines. I just create regions now to do MASSPROP to get the centroid. I'm just looking for a more efficient way of finding and dimensioning the centroid.

Thank you

0 Likes
Message 10 of 17

BillAllenSE
Collaborator
Collaborator

I entered the following in the command line:

_.point (enter)

_.gcen (enter)

I then hovered over the polyline and it place a point at the cg.

Is there a "macro record" feature in ACAD similar to Excel? If so, I can record this as a macro for later use, because I will never remember this. 🙂

Thank you very much

 

0 Likes
Message 12 of 17

BillAllenSE
Collaborator
Collaborator

That didn't work for me. I got a "properties close" function instead. Even though "prc" is not in my alias list, but prclose is.

0 Likes
Message 13 of 17

Kent1Cooper
Consultant
Consultant

Change the command name from PRC to something else if you want.  Or don't let it autocomplete the command name -- back off the "LOSE" it puts on.

Kent Cooper, AIA
0 Likes
Message 14 of 17

Kent1Cooper
Consultant
Consultant

@BillAllenSE wrote:

.....

Is there a "macro record" feature in ACAD ...? ....


It's called the Action Recorder -- the >ACTRECORD command<.

Kent Cooper, AIA
0 Likes
Message 15 of 17

BillAllenSE
Collaborator
Collaborator

Thank you very much. This looks easy enough that even I might be able to do this.

🙂

0 Likes
Message 16 of 17

Sea-Haven
Mentor
Mentor

The action recorder is a bit limited in that it is not editable in a more complex situation. A failure in a way by Autodesk that they did not do Like Excel and write VBA code for macros. Any experienced lisper will probably not use the macro record, a side note one Cad package did write VBA code as its macro record.

 

My suggestion is that you save the lisps like the one suggested here in a single location, you can make a Custom lisp that gets loaded every time you start your CAD, I have one called Autoload.lsp, to load it you just do APPLOAD and then save to the "Start up suite".

 

Ok now what is in your custom lisp is a link to your other Lisps using the "Autoload" function this will demand load your other lisp for you when you type the command name. Here is an example. Also inside Autoload is some 27 defuns or macros like the one here, where they are only a couple of lines long.

 

 

 

(autoload "COPY0" '("COPY0"))
(autoload "COPYCOMMAND" '("ZZZ"))
(autoload "COVER" '("COVER"))
(autoload "DIMFLIP" '("DIMFLIP"))
(autoload "DRAWXFALL" '("DRAWXFALL"))
(autoload "DRAWPIPE" '("DRAWPIPE"))
(autoload "EDITRL" '("EDITRL"))
(autoload "EDITXSECT" '("EDITXSECT"))
(autoload "EDITLSECT" '("EDITLSECT"))
(autoload "FLIPLINE" '("FLIPLINE"))
(autoload "Goto-LAYOUT" '("GOTO"))

 

 

 

Load "Lispname" "commandname"

 

Also the lisps have been saved in a support path so you can use a short name without the full path being needed. 

OPTIONS, FILES, Support paths so add your location of all your saved lisp files. eg D:\CADSTUFF\LISPS.

Options Bricscad, Acad is similar

SeaHaven_2-1714781729093.png

 

Happy to help you make a POP menu if you want, hope this information was useful.

 

A better way as you are probably going to get more and more lisps is to use say a POP menu, these can be made using Notepad and are text files with .mnu file name. They are loaded using Menuload.

 

This menu made over many years and gets the odd 1-2 addition a year has 100+ lisps behind it.

SeaHaven_0-1714781456239.png

 

 

 

0 Likes
Message 17 of 17

BillAllenSE
Collaborator
Collaborator

Thanks, everyone for the help. Not only did I solve my immediate problem (learning the gcen command), but also got some direction to get started in learning to program. I copied the links and put them in my reading list.

 

Again, thank you all very much for your responses.

0 Likes