Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Advice regarding sub procedures and wrappers.
owen_sparks
Member, Developers Posts: 2,660 PRO
Hi folks.
I'm taking my first tentative steps with featurescript and would like to see if I'm even thinking in the right direction. (Please forgive the pseudo VBA like syntax, it's all I'm currently familiar with; so just using it here to hopefully convey the intent.) I'm not looking for help with actual code, more a discussion on workflow.
(Scenario 1) Making code cleaner / easier to use later.
Say we see a future where we'll need a lot of hexagons in our lives. Further assume we'll want to dimension them "flat to flat"not as a "circumscribed polygon".
Now we have a perfectly good polygon tool in fs and we can use it like this...
I'm taking my first tentative steps with featurescript and would like to see if I'm even thinking in the right direction. (Please forgive the pseudo VBA like syntax, it's all I'm currently familiar with; so just using it here to hopefully convey the intent.) I'm not looking for help with actual code, more a discussion on workflow.
(Scenario 1) Making code cleaner / easier to use later.
Say we see a future where we'll need a lot of hexagons in our lives. Further assume we'll want to dimension them "flat to flat"not as a "circumscribed polygon".
Now we have a perfectly good polygon tool in fs and we can use it like this...
skRegularPolygon(sketch1, "polygon1", {
"center" : worldToPlane(MySketchPlane, startPosition),
"firstVertex" : worldToPlane(MySketchPlane, startPosition) + vector(FeatureWidth/2 , 0 ) * millimeter,
"sides" : 6
});
...and if we want to dimension flat to flat we can tell it to do so by re-dimensioning the length before making the polygon ...
if Dimension_Flat_to_Flat_Not_Point_to_Point = true then HexagonWidth = HexagonWidth / cos(30 * degree)
skRegularPolygon(sketch1, "polygon1", {
end sub
Then just park this sub procedure somewhere and forget about it.
Next time we want a 25mm hexagon in a featurescript we can ask for it in a singe line of code:-
JustGiveMeAHexagon(sketch1,Startpoint,25,true)
(Scenario 2) Reusable common modules.
Can we call featurescript sub procedures from within other featrescripts?
Step 1 - Create separate library of similar procedures to:-
(a) sub Create_a_plane_from_a_point_and_a_body(point,plane)
(b) sub Create_a_sketch_on_a_plane(sketch)
(c) sub JustGiveMeAHexagonOnASketch(as above)
(d) sub JustGiveMeACircleOnASketch
(d) sub ExtrudeMyEntities(entities, depth)
Again park all this and forget about it.
Step 2 - Mix 'n' match subs as required
Sub BuildMeaHexagonalHoleHere(point,body,hexwidth,hexdepth)
MyPlane = Create_a_plane_from_a_point_and_a_body(point,body)
MySketch = Create_a_sketch_on_a_plane(Myplane)
MyHexagon = JustGiveMeAHexagonOnASketch(Mysketch,point,Hexsize,true)
call ExtrudeMyentities(MyHexagon,hexdepth)
end sub
Step 3 - Either stop now if it's to be called by other code, or drop it in its own feature
Sorry for the long waffly post. If you made it this far without losing the will to live I'd love to hear your opinions.
Cheers,
Owen S.
...and if we want to dimension flat to flat we can tell it to do so by re-dimensioning the length before making the polygon ...
//Adjust "Featurewidth" variable as we want "flat to flat" on hex nut not "point to point"
FeatureWidth = FeatureWidth / cos(30 * degree);
However this is all a bit messy and next time I come to use it my recollection of trig lessons from last century will be even dimmer.
So can we build a sub-procedure / wrapper to do this for us? Along these lines:-
Public sub JustGiveMeAHexagonOnASketch(TargetSketch as sketch,centrepoint as vector, HexagonWidth as number, Dimension_Flat_to_Flat_Not_Point_to_Point as boolean)
However this is all a bit messy and next time I come to use it my recollection of trig lessons from last century will be even dimmer.
So can we build a sub-procedure / wrapper to do this for us? Along these lines:-
Public sub JustGiveMeAHexagonOnASketch(TargetSketch as sketch,centrepoint as vector, HexagonWidth as number, Dimension_Flat_to_Flat_Not_Point_to_Point as boolean)
if Dimension_Flat_to_Flat_Not_Point_to_Point = true then HexagonWidth = HexagonWidth / cos(30 * degree)
skRegularPolygon(sketch1, "polygon1", {
"center" : yada yada
"firstVertex" : worldToPlane(MySketchPlane, startPosition) + vector(FeatureWidth/2 , 0 ) * millimeter,
"sides" : 6
});
end sub
Then just park this sub procedure somewhere and forget about it.
Next time we want a 25mm hexagon in a featurescript we can ask for it in a singe line of code:-
JustGiveMeAHexagon(sketch1,Startpoint,25,true)
(Scenario 2) Reusable common modules.
Can we call featurescript sub procedures from within other featrescripts?
Step 1 - Create separate library of similar procedures to:-
(a) sub Create_a_plane_from_a_point_and_a_body(point,plane)
(b) sub Create_a_sketch_on_a_plane(sketch)
(c) sub JustGiveMeAHexagonOnASketch(as above)
(d) sub JustGiveMeACircleOnASketch
(d) sub ExtrudeMyEntities(entities, depth)
Again park all this and forget about it.
Step 2 - Mix 'n' match subs as required
Sub BuildMeaHexagonalHoleHere(point,body,hexwidth,hexdepth)
MyPlane = Create_a_plane_from_a_point_and_a_body(point,body)
MySketch = Create_a_sketch_on_a_plane(Myplane)
MyHexagon = JustGiveMeAHexagonOnASketch(Mysketch,point,Hexsize,true)
call ExtrudeMyentities(MyHexagon,hexdepth)
end sub
Step 3 - Either stop now if it's to be called by other code, or drop it in its own feature
FeatureScript 376;
import(path : "onshape/std/geometry.fs", version : "376.0");
annotation { "Feature Type Name" : "Hex-Hole-In-Body" }
export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
precondition
{
// Define the parameters of the feature type
//Ask for point, body and hex-dimensions here
//Ask for point, body and hex-dimensions here
}
{
// Define the function's action
BuildMeaHexagonalHoleHere(point,body,hexwidth,hexdepth)
BuildMeaHexagonalHoleHere(point,body,hexwidth,hexdepth)
});
Sorry for the long waffly post. If you made it this far without losing the will to live I'd love to hear your opinions.
Cheers,
Owen S.
Business Systems and Configuration Controller
HWM-Water Ltd
HWM-Water Ltd
0
Comments
export function xxx(context is Context, id is Id, blah, blah, blah)
yes, your approach is perfectly valid. I call these types of functions 'builder' functions. they accept a sketch, and then they draw various stuff on it and return.