PDA

View Full Version : "CENTROID" Osnap Feature



kryptonite_186
2004-12-23, 08:44 PM
Wouldn't it be great if you could have a "CENTROID" object snap feature. When it is used it could repeatedly prompt you for as many points as you like, which you may select using any type of osnap that you like (i.e. endpoint, midpoint, intersection, etc.), and when you stop selecting points it could calculate and snap to the centroid of these selected points. It could work much like the "FROM" osnap feature.

kryptonite_186
2004-12-28, 11:26 PM
Actually, this is not a new idea either. Currently, using the CAL command transparently ('CAL), you can enter an equation that will snap to the centroid of any number of specified points.

For example, the equation (end+end+end)/3 gives you the centroid of 3 selected endpoints. You can also alternate between the different osnap types (i.e. midpoints, intersections, centers, etc.) and specify varying divisors. For example, the equation (mid+mid+end+end)/4 gives you the centroid of 2 selected midpoints and 2 selected endpoints (the centroid of 4 points).

However, the setup involved with this process (writing the equations) is very time consuming since you have to specify before hand exactly how many points you will be selecting and what type of osnap you will be selecting them with. Plus it uses the old pick box style of snapping to these points (I beleive this was upgraded to the current osnap marker in ACAD - R14).

Anyway, I think it would be great if there were a way to run these calculations quickly and without the hassle.

kryptonite_186
2005-01-11, 07:39 PM
I meant to share the attached lisp file at the original posting of this thread. It allows you to run a routine I call mid2. I personally have this set on my computer to run "transparently" at the press of the F12 key. This routine works like an osnap feature. It prompts the user for 2 points and then returns the midpoint coordinate between those points. It saves you from actually drawing a line between the 2 points and erasing it later when you are trying to find the midpoint between the 2 points.

The idea of a "CENTROID" osnap would expand the concept of this routine to allow you to find the average center of additional (more than 2) points.

If you have difficulty using the mid2 routine remember to run it "transparently" ( 'mid2 ) while in another command and it will allow you to automatically snap to the calculated midpoint as described above. I hope you all find the mid2 routine as helpful as I have and I look forward to your thoughts about the CENTROID osnap.

ntaylor
2005-01-11, 09:31 PM
In case some people are unaware 2005 has a new midpoint between 2 points snap where you can select 2 points with or without other snaps and it selects the midpoint between the selected points. The idea of the centroid of an unlimited amount of points is a great idea and a slight variation could be where you select near a polyline and it works out the centroid from all the vertices.
Regards - Nathan

bbapties
2005-01-11, 09:45 PM
you could make it give you the centroid of an closed "object" (pline)

example: ....

command: select points or [Object]

great wish list item with the multiple points!!...:beer:

kryptonite_186
2005-01-12, 11:40 PM
In case some people are unaware 2005 has a new midpoint between 2 points snap where you can select 2 points with or without other snaps and it selects the midpoint between the selected points. The idea of the centroid of an unlimited amount of points is a great idea and a slight variation could be where you select near a polyline and it works out the centroid from all the vertices.
Regards - Nathan

I actually had a lisp routine that would do just what you said, that is it calculated the centroid of a polyline based on its vertices, but it quit working when I upgraded from 2000 and I haven't had the time to figure out how to fix it :( . I often consider going back to 2000 since that routine was so helpful.

Anyway, glad to hear a response from both of you. I was beginning to think no one knew what a centroid was ;) .

lcamara
2005-01-14, 09:13 PM
I actually had a lisp routine that would do just what you said, that is it calculated the centroid of a polyline based on its vertices, but it quit working when I upgraded from 2000 and I haven't had the time to figure out how to fix it :( . I often consider going back to 2000 since that routine was so helpful.

Anyway, glad to hear a response from both of you. I was beginning to think no one knew what a centroid was ;) .
If you've already got something, you could just post it and someone on the forum should be able to help you debug it. If it was working before, it's probably just due to one of the changes from 14 to 2000.

Some comments on your midpoint command: you should really get into the habbit of using private variables. By limiting the scope of your variables, you don't have to worry about accidentally overwriting something, or setting your variables back to nil to "clean up". It's also helpful to name the variables so they reflect their content. This makes your code easier to read. See my attached file ("mid2a.lsp") for a modified version of your command.

One thing I learned from your routine - I didn't know you could use the "command" function to return a value. I thought it automatically interrupted the current command and started a new one. In our midpoint command, I just used the midpoint as the return value. The problem with this was that I needed to set osnap to 0 first. I was considering the idea of using a reactor to set this back. Luckily, I never got around to that. :) I attached my updated routine for reference ("mid.lsp"). It was written to start drawing a line when run as a command (it checks the "cmdactive" variable). That part of its functionality might still be useful to some, even in 2005.

ntaylor - thanks for pointing out that new "mid between 2 pts" snap. It would have taken me a while to notice that one on my own.

kryptonite_186
2005-01-14, 10:28 PM
Thanks for your response lcamara.



Some comments on your midpoint command: you should really get into the habbit of using private variables. By limiting the scope of your variables, you don't have to worry about accidentally overwriting something, or setting your variables back to nil to "clean up".
Since this routine was being used as an osnap and was being used in conjunction with other commands, I chose to use unique variables. Plus it was such a simple line of code it didn't get to confusing (for me anyway). Also, I don't remember the specifics, but it was interfering with another routine I had and I didn't want the same to happen to my fellow AUGI users so I changed the variables to what I had posted (mid2.lsp), but thanks anyway for your concern over my coding style (I'm really touched ;) ).

I am posting a lisp file (centroid.lsp) that contains a function that used to work back in the 2000 release that would find the centroid of a closed polyline. This quit working with the 2002 release and up, but with 2000, a lisp user could create a selection set of a polyline under a variable called "Pent" and the routine would calculate the centroid of the polyline and assign the coordinate value to a variable called "centroid". That value is based on the vertices of the polyline so if the polyline contains an arc the value does not give the "true" centroid of the polyline. Anyway, if someone can fix it please send me a PM (since this is a wish list and not a help forum) and maybe we can put it all back together and post it for a nice demonstration of how this could actually work.

lcamara
2005-01-14, 11:01 PM
Thanks for your response lcamara.

Since this routine was being used as an osnap and was being used in conjunction with other commands, I chose to use unique variables. Plus it was such a simple line of code it didn't get to confusing (for me anyway). Also, I don't remember the specifics, but it was interfering with another routine I had and I didn't want the same to happen to my fellow AUGI users so I changed the variables to what I had posted (mid2.lsp), but thanks anyway for your concern over my coding style (I'm really touched ;) ).

Hey! Are you making fun of my OCD tendencies? I'll admit it - I'm AR! :)

That said, part of my point was that if you make the variables private, you don't have to worry about the names conflicting with other programs. If you have two functions that use "OS" as a variable, one can call the other & still not mess up the values (as long as they're declared private in the defun line).

BTW, the code you posted doesn't actually calculate the centroid (center of area/mass) although it would give you the same point for rectangles (of any angle) and maybe for all regular polygons, but it will be wrong for irregular polygons since it only looks at the max & min x & y values of the polygon. Basically it gives you the center of the polygon's extents.

kryptonite_186
2005-01-17, 04:36 PM
BTW, the code you posted doesn't actually calculate the centroid (center of area/mass) although it would give you the same point for rectangles (of any angle) and maybe for all regular polygons, but it will be wrong for irregular polygons since it only looks at the max & min x & y values of the polygon. Basically it gives you the center of the polygon's extents.
Hey your right. It just gives the average coordinate of all the vertices. Never noticed that. Actually, I got that particular function from a friend. I used it to hatch ceiling grids that would auto-center the hatch in the boundary by setting your UCS to the calculated point. Also used it as a snap but it never worked as well as the mid2.

It would still be nice if it worked though. That way we would have something to post to demonstrate.