CamBam
Resources => Scripts and Plugins => Topic started by: lloydsp on June 19, 2014, 20:40:02 pm
-
Ok... I DID have the time, because I had a long job running, had all today's design work done, and was only listening for cutters hitting the clamps!
The attached script and sample file explores calculating the enclosed area of an irregular polyline, and calculates its perimeter length, as well.
It uses the ancient "Surveyor's formula".
It presently only works on polys consisting entirely of line segments, and they must be closed polys.
Some work could be done to manage calculating the segment of a circle in order to handle arcs, as well; but I really don't have time for that.
So, if you have a polyline which has arcs in it, do a "remove arcs" on it before trying this.
It will bark at you for un-closed polylines, or those containing any arcs.
Just unzip the file, and place the resulting .vbs file in your CamBam/System/Scripts folder.
Below. Updated 06/20/14 for any closed shape, including circles, and change the name, since it no longer requires polylines, only closed shapes.
LLoyd
-
http://en.wikipedia.org/wiki/Shoelace_formula (http://en.wikipedia.org/wiki/Shoelace_formula)
Sorry - I had to look it up!
Bob
-
<G>
It's really a simple algorithm. I'm looking at ways to handle arc segments, also. It's not that hard, but will take some thoughtful coding to make sure it's accurate, and properly handles the bulges.
Lloyd
-
Updated...
It was WAY simpler to deal with arcs than I expected.
Updated version and testing .cb file below. Now handles any closed polyline (although I have not tested what happens with overlaps and/or crossovers....)
LLoyd
ps have removed attachments for all but the first post. Updates are there.
-
Works fine, Tested some objects against DraftSight Area/perimeter Tools for comparison.
Dave.
-
Thanks, Dave. That's good confirmation.
I might add that to the toolbar as a plugin. My most frequent use of it would be to calculate the weights of various parts.
Many of my mechanisms have 'balanced' parts, or use scales the limits of which can be exceeded if the weights of holders, etc. become too much.
It's easy to calculate the mass of a regular shape, but not oddball geometries.
LLoyd
-
Updated again (in top post of thread).
More testing added for types, circles now handled.
Except for any possible bugs, I _think_ this is as far as this needs to go until it's made into a plugin.
Please test. Suggestions welcome.
LLoyd
-
BTW,
Upon thinking of the potentials for this, I think it could be turned into a general-purpose function to calculate areas, volumes, and mass based upon specific densities of materials.
I'll work on that as time permits. Ideas?
Lloyd
-
Hello Lloyd
Nice job :D (but I realy don't understand the math formulas on the wiki :o :-[)
++
David
-
Lloyd
Yes I think to add these functions to CamBam would good idea, As I've often had to use other programs
to do just that.
Perhaps you could incorporate a user editable .txt file for the material densities, And you would need
to supply the 2D objects with a thickness (or Z value ) to calculate the volume from a popup window.
Dave.
-
Thanks, David.
The formula is a lot simpler than you can make it in your mind. All it does is divide up the whole object into little right triangles, and sum their area.
I'm working up some concept documents to turn this into an area/volume/mass calculator for 2D objects.
Lloyd
-
Yes, Dave.
The concept already includes a data file for densities of common materials, plus the ability to edit it.
Since I don't intend to take this past 2.5D, it will be necessary for the user to enter the material thickness.
I've written some plugins that use user-input boxes, so that shouldn't be a problem.
Thanks for the input.
Lloyd
-
So, on another wet day (they don't call it the "Lake District" for nothing), for something semi interesting to do, I made this into a plugin and added Splines to the objects it can handle.
It appears in the 'Tools' menu as "Area"
Lloyd, what was the crack with your planned additions re volume and mass ?
Edit: attachment removed see later post.
-
Hello eddy
I just try this plugin before to add it to the web site and I fall on a bug ; if you select an open polyline then run the plugin, you first get "Drawing object ID#x is not closed: skipped', then when you click OK, a second message appears ; 'There were no closed objects selected', then after clicking OK, CB run in an infinite loop (Cambam is thinking) and I can't cancel the job, except by closing CB. (tested on V1.0 R13)
++
David
Edit: texts are not translatable (except the menu, of course)
-
Nothing yet, Eddie. I did some searching the other evening, but didn't find the mass/density tables I _thought_ I intended to use.
In the middle of a four-day 'rush job' right now, but as soon as I spring free, I'll do some more looking-around.
Lloyd
-
Thanks, Dave. That's good confirmation.
I might add that to the toolbar as a plugin. My most frequent use of it would be to calculate the weights of various parts.
Many of my mechanisms have 'balanced' parts, or use scales the limits of which can be exceeded if the weights of holders, etc. become too much.
It's easy to calculate the mass of a regular shape, but not oddball geometries.
LLoyd
I often have to calculate the weight of cast lead objects. Since I am almost always working from a 3D model in ViaCad I use its built in (verify volume) function and use .41lbs per cubic inch. Then * 16 for ounces. I have a little cheater spread sheet in the tools on my desk top that I can just plug a bunch of values into when I have a lot of grunt work for various sizes of the same object.
-
Solidworks has the same area and volume calcs plus densities of lots of materials.
-
I know many CAD programs have this facility but CamBam did not.
David,
Bugs should be fixed now. I tried to follow Lloyd's script as close as possible but added the, view.StartThinking(false); message, unfortunately I forgot to add, view.StopThinking(); when an error was found.
I've changed the program logic slightly too.
1. You can select multiple objects
2. Translation code added to messages
Edit: attachment removed, see later post for new version
-
Hi Eddy
Just ran the plugin and selected a few shapes, and what would be handy is a cancel button (in case you have selected too many entities and wanted to abort) , and instead of popping up a new message box for each entity perhaps listing them on the first message box.
Dave
-
Dave,
I've never had luck adding a Cancel button to the thinking message.
If you use, view.StartThinking(true); it adds the Cancel button but I think you have to add an event handler for this. Do you have any example code to make this work ?
-
HI Eddy
No when I came to do this in the collision detector I used a background worker because i wasn't sure how to do it either
but I think CB runs a task on a different thread (threading) and I think now I could work it out given a little time
and I will look at it as soon as I work out what to do with the break edges plugin as with using a style as I've discovered that while the tool number and profile are specified in the style the cutter V angle is not and neither is the tool library that the tool data comes from.
CB looks for the tool library in the part and if the field is null then it uses the default library, which is ok if your tool is in the default library, but I've organised my libraries by tool type drills in one and end mills in another, and so the style library data (tool index) would point to the wrong tool if you don't enter the tool library that you use in the part. so it looks like I'll have to add another drop down to enter the tool library to go along with the style library, which sorta seems the long way around. ::)
Dave
-
Hello Eddy
I falls on another problem ..
A new key is added in the translation list for each new object that is 'measured' with the plugin.
++
David
-
David,
I don't know why, I've added the source project if you have time to look. Everything is in Class1.cs and Notepad++ is fine to read it with.
-
Hello
Ok, I'll have a look ... ;)
++
David
-
Hello
Currently, I can't find how that works with variable in the text ??? ... but I found another problem with translation in the Spiral Toolpath from Profile plugin ; there is also texts that are added for nothing.
I just send an email to Andy to know if he can help.
++
David
-
I don't know why
I know why, but I don't how how to use the right way as in:
"The file '{0}' already exists! Overwrite existing file?"
where the '{0}' is replaced by a variable in the translation list
the reason because your code adds a new entry each time is because, each time the text is different because the value in ent.ID and other variable change, and off course, each time you get a different string to translate.
MessageBox.Show(TextTranslation.Translate("Area enclosed by polyline ID# " + ent.ID + " = " + string.Format("{0:N6}", Math.Abs(Area)) + " ^2 drawing units." + Environment.NewLine + "Perimeter Length of polyline ID# " + string.Format("{0:N6}", perimeter) + " drawing units."));
I think that currently, the way is to split each 'static' text as an entry in the translation list, so the code become:
MessageBox.Show(TextTranslation.Translate("Area enclosed by polyline ID# ") + ent.ID + " = " + string.Format("{0:N6} ", Math.Abs(Area)) + TextTranslation.Translate("^2 drawing units.") + Environment.NewLine + TextTranslation.Translate("Perimeter Length of polyline ID# ") + string.Format("{0:N6} ", perimeter) + TextTranslation.Translate("drawing units"));
I use one TextTranslation.Translate() function per 'static' text and I don't translate the texts that can change.
I also move the space before the "^2 drawing units." and " drawing units." string to the end of the previous text to avoid text that start with a space.
I remove the dot at the end of "drawing units.", because "drawing units" already exists in the translation list, so it will not be translated again.
Tested with a visual basic translation of this line of code .. and it seems to works.
++
David
Edit: What is your C# version ? I can't open the .sln file with mine (visual C# 2010 express)
-
David,
Thank you for finding the answer, I'll have to remember that trick in future.
version 1.3 attached.
I use VS 2015 Community
-
HI Eddy
PM sent maybe twice ::) I've answered re the cancel button.
-
Hello Eddy
I just see another little bug (exist in previous version), the polyline # is missing in the perimeter line. (maybe it's not needed to write the ID twice ?)
(http://www.atelier-des-fougeres.fr/Cambam/Aide/Plugins/Area/UI_EN.png)
++
David
-
David,
You testing is very good ;)
I just updated the v1.3 in my previous post, it should be okay now (I hope)
-
Dear Eddy
I have had a need to calculate second moments of area of polygon shapes, so I have take the liberty to update your Area plugin as it provided most of the basic infrastructure. I have the V1.2 source, and I think I have included. the v1.3 updates (?? - I think the changes only related to the TextTranslation in the result dialog). I am including here V1.4 for testing.
thanks
Geoff
-
Geoff,
As always, that's fine, it was Lloyd's script in the first place anyway ;)
The calculations work correctly when the object is centred at 0,0 (I tried a rectangle and circle)but if they are moved away from that position then the numbers change, is that how it should be ?
-
Dear Eddy
The second moments of area are defined for a nominated axis, often the centroidal axes that are parallel to the cartesian X or Y axis, but it doesn't have to be. To get the correct Ixx and Iyy for this case you need to place the centroid of the shape at the origin (X=0, Y=0). If you move the centroid to some other location then the Ixx and Iyy will change according to the Parallel Axis Theorem.
Does this explain?
Geoff
-
Geoff,
Thanks, I see that but should the plugin not adjust the numbers so that an object's centroid becomes a virtual 0,0 ?
In other words, rather than moving the object centroid to 0,0 we should move 0,0 to the object's centroid.
-
Dear Eddy
Yes, we could do that, but it would then not be possible to compute Ixx and Iyy at some other axes (like the bottom edge of a rectangular shape) -without some additional manual computation (like Ixx + A*d^2)
Geoff
-
Geoff,
I chose a bad example by using the centroid because rotation can be about any point not just the centre, e.g. a pendulum.
In general terms then we should be able to draw an object anywhere in the CamBam window and specify the point of rotation which shall then become virtual 0,0
Failing that, some instructions would have to accompany the plugin to indicate centre of rotation needs to be located at CamBam's 0,0
The plugin seems to working as it should, with the regular polygons I used for testing, what I'm on about is the user interface.
-
Dear Eddy
We are getting into deep water here - it is a bit more complicated that I have indicated. It might be best to refer anyone interested to a good description of second moments of area e.g Wikipedia (https://en.wikipedia.org/wiki/List_of_second_moments_of_area (https://en.wikipedia.org/wiki/List_of_second_moments_of_area)) rather than try to to it ourselves and mess it up!
Suffice to say: The second moment of area of a shape is defined about an axis in the plane of the shape. For symmetrical shapes (like rectangles) it will typically be aligned with one of the sides, it may be through the centroid but may be located elsewhere, like along one of the edges, aligned with X or Y. Remember that the Second Moment of Area is a property of planar shapes, while it can be related to Moment of Inertia that is a property of solids (with mass), it is not the same.
For the plugin then:
- To get the Ixx and Iyy about the centroidal axis (a common task) then the shape must be placed so that its centroid is at the origin. You can use the Centroid plugin to locate it.
- To get the Ixx and Iyy about some other axes then the shape must be placed so that these axis are aligned with X=0 and Y=0 respectively
Geoff
-
Hello Geof
I just see that the id# and the = are missing on the 2th line ;)
++
David
-
David,
Well spotted, I think we shall have to let Geoff make the changes.
-
Thanks & OK, here is v1.4.1
Geoff
-
Hello
Thanks ;)
++
David