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.
Drive an Instantiator via an Enum input?
owen_sparks
Member, Developers Posts: 2,660 PRO
Hi folks.
I'm writing a little FS that will make pockets in a target part for threaded "Heatserts" and bring in a part representing the insert.
Instead of building the parts within FS it imports a solid "Void" from and external file, transforms it into place and then boolean removes it from the target part. Likewise for the insert itself.
File here:-
clicky

It's working nicely (thanks Neil) with the insert type hard coded into an Instantiator, so it'll put in an M6 insert no matter which option you chose! (M6 was always my favourite "M" but it'd be nice to be able to use others!)
I added a choice of insert types via the enum at the top of the feature, but am unsure how to pass that value to the Instantiator?
I'd expect to be able to declare a variable, set that depending on the enum choice and use the variable in place of the "HeatsertVoidForM6x8_5mmx8mm::build" in this line of code:-
Help!
Sorry for the newbie questions; it's like being a toddler again!
Cheers,
Owen S
I'm writing a little FS that will make pockets in a target part for threaded "Heatserts" and bring in a part representing the insert.
Instead of building the parts within FS it imports a solid "Void" from and external file, transforms it into place and then boolean removes it from the target part. Likewise for the insert itself.
File here:-
clicky

It's working nicely (thanks Neil) with the insert type hard coded into an Instantiator, so it'll put in an M6 insert no matter which option you chose! (M6 was always my favourite "M" but it'd be nice to be able to use others!)
I added a choice of insert types via the enum at the top of the feature, but am unsure how to pass that value to the Instantiator?
//Set Which PartStudios to use for Instantiator based on user input
if (definition.InsertTypeSelected == InsertType.M3x5_4x8)
{
debug(context, "M3 Selected");
//Help! I want to set a variable here so pick which import to use!
}
if (definition.InsertTypeSelected == InsertType.M6x8_5x8)
{
debug(context, "M6 Selected");
}
I'd expect to be able to declare a variable, set that depending on the enum choice and use the variable in place of the "HeatsertVoidForM6x8_5mmx8mm::build" in this line of code:-
addInstance(instantiatorVoid, HeatsertVoidForM6x8_5mmx8mm::build, { "transform" : toWorld(coordSystem(skPlane))} );Help!
Sorry for the newbie questions; it's like being a toddler again!
Cheers,
Owen S
Business Systems and Configuration Controller
HWM-Water Ltd
HWM-Water Ltd
Tagged:
0
Comments
for (var i = 0; i < TargetPointCount; i += 1) { debug(context,"LoopCounter:- " ~ i); var skPlane = evOwnerSketchPlane(context, { "entity" : TargetPoints[i] }); skPlane.origin = evVertexPoint(context, { "vertex" : TargetPoints[i] }); var instantiatorVoid = newInstantiator(id + i + "instantiate"); if (definition.InsertTypeSelected == InsertType.M3x5_4x8) { //debug(context, "M3 Selected"); //Help! I want to set a variable here so pick which import to use! addInstance(instantiatorVoid, HeatsertVoidForM3x5_4mmx8mm::build, { "transform" : toWorld(coordSystem(skPlane))} ); } if (definition.InsertTypeSelected == InsertType.M6x8_5x8) { //debug(context, "M6 Selected"); addInstance(instantiatorVoid, HeatsertVoidForM6x8_5mmx8mm::build, { "transform" : toWorld(coordSystem(skPlane))} ); } instantiate(context, instantiatorVoid); //(3) Boolean remove Void from TargetPart opBoolean(context, id + i + "boolean1", { "tools" : qCreatedBy(id + i + "instantiate", EntityType.BODY), "targets" : definition.TargetBody, "operationType" : BooleanOperationType.SUBTRACTION }); //(4) Import HeatSert and //(5) Transfor Heatsert into place if checkbox is selected if (definition.AddInsert==true) { var instantiatorPart = newInstantiator(id + i + "instantiate2"); if (definition.InsertTypeSelected == InsertType.M3x5_4x8) { //debug(context, "M3 Selected"); addInstance(instantiatorPart, HeatSert_M3x5_4mmx8mm::build, { "transform" : toWorld(coordSystem(skPlane))} ); } if (definition.InsertTypeSelected == InsertType.M6x8_5x8) { //debug(context, "M6 Selected"); addInstance(instantiatorPart, HeatSert_M6x5_8mmx8mm::build, { "transform" : toWorld(coordSystem(skPlane))} ); } instantiate(context, instantiatorPart); //(6) Set some part properties. (Maybe!) }There has to be a better wayOwen S
HWM-Water Ltd
const boxInstance = addInstance(instantiator, BOX::build, { "configuration" : { "Length" : length, "Width" : width, "Height" : height, "Rotate" : rotate } });export enum Sizes { annotation { "Name" : "M6" } M6, annotation { "Name" : "M8" } // my favourite M8 } annotation { "Feature Type Name" : "My Feature" } export const myFeature = defineFeature(function(context is Context, id is Id, definition is map) precondition { annotation { "Name" : "My Enum" } definition.size is Sizes; } { var config = toString(definition.size); }Hi Neil,
Thanks for the replies. I may be missing the point.
Are we talking cross purposes here, I need the instantiator to use a different "import" for each enum type?
PartStudio_Component::build, { "transform" : toWorld(coordSystem(skPlane))} );</code>//Import all options //Insert M3 x 5.4mm Diameter x 8mm Length HeatSert_M3x5_4mmx8mm::import(path : "46a3854cbda96c62e0d2c699/74e51702442e9bb074c1f5f1/b169419f94a85683403aec80", version : "e52464d4091d572a1b9ff575"); HeatsertVoidForM3x5_4mmx8mm::import(path : "46a3854cbda96c62e0d2c699/db1c68975d587fd3410ee360/0d1eec73a2af73cb44804e51", version : "9d14834abcda97000de0a1a7"); //or //Insert M6 x 8.5mm Diameter x 8mm Length HeatSert_M6x5_8mmx8mm::import(path : "46a3854cbda96c62e0d2c699/62894ed0d7e00153dc3aa9b7/fd409ff7138ba0d1db28a644", version : "42da9a10f54abcc8248f75c4"); HeatsertVoidForM6x8_5mmx8mm::import(path : "46a3854cbda96c62e0d2c699/62894ed0d7e00153dc3aa9b7/0d1eec73a2af73cb44804e51", version : "97487f7145d53c7631bb0c78"); </pre><div><br></div><span>Pseudo code:-<br><br><pre class="CodeBlock"><code> //Now decide which to PartStudios to use for Instantiator based on user input Var PartStudio_Void as partstudio Var PartStudio_Component as partstudio<br> if (definition.InsertTypeSelected == InsertType.M3x5_4x8) { debug(context, "M3 Selected"); PartStudio_Void = HeatsertVoidForM3x5_4mmx8mm; //<< this is not correct<br> PartStudio_Component = HeatsertVoidForM3x5_4mmx8mm; //<< this is not correct<br> } if (definition.InsertTypeSelected == InsertType.M6x8_5x8) { debug(context, "M6 Selected"); PartStudio_Void = Different Part Studio <br> } /// then later buried in the code inside a loop and never looked at again addInstance(instantiatorVoid, PartStudio_Void::build, { "transform" : toWorld(coordSystem(skPlane))} );<br> // later still <pre class="CodeBlock"><code>addInstance(instantiatorPart,
Is we can set a variable at the top of the code it's be much easier to maintain that sifting through the whole when new parts are added.
If I'm making no sense don't be shy in telling me so!
It's also Assembly Configs Webinar time so I think I'll leave this until the morning!
Cheers, Owen S.
HWM-Water Ltd
HWM-Water Ltd
HWM-Water Ltd
HWM-Water Ltd
CIRCLE::import(path : "b96ecf06f68f8a1ec70a1f3c", version : "a4d31802f9fe58b2f2e5d2fd"); SQUARE::import(path : "4601f86b931b27691da4f558", version : "ffe1bfe7eadb35f07573ea5f"); export enum ShapeType { annotation { "Name" : "Circle" } CIRCLE, annotation { "Name" : "Square" } SQUARE } annotation { "Feature Type Name" : "Enum Test" } export const myFeature = defineFeature(function(context is Context, id is Id, definition is map) precondition { annotation { "Name" : "Type" } definition.shape is ShapeType; } { var imports = { CIRCLE : CIRCLE::build, SQUARE : SQUARE::build }; const instantiator = newInstantiator(id + "inst", {}); const shapeInstance = addInstance(instantiator, imports[toString(definition.shape)], {}); instantiate(context, instantiator); });You still have to import every Part Studio but it means the code is cleaner.HWM-Water Ltd
CIRCLE::import(path : "b96ecf06f68f8a1ec70a1f3c", version : "a4d31802f9fe58b2f2e5d2fd"); SQUARE::import(path : "4601f86b931b27691da4f558", version : "ffe1bfe7eadb35f07573ea5f"); export enum ShapeType { annotation { "Name" : "Circle" } CIRCLE, annotation { "Name" : "Square" } SQUARE } const imports = { CIRCLE : CIRCLE::build, SQUARE : SQUARE::build }; annotation { "Feature Type Name" : "Enum Test" } export const myFeature = defineFeature(function(context is Context, id is Id, definition is map) precondition { annotation { "Name" : "Type" } definition.shape is ShapeType; } { const instantiator = newInstantiator(id + "inst", {}); const shapeInstance = addInstance(instantiator, imports[toString(definition.shape)], {}); instantiate(context, instantiator); });const imports = {<br> ShapeType.CIRCLE : CIRCLE::build,<br> ShapeType.SQUARE : SQUARE::build<br> }; // Later... addInstance(instantiator, imports[definition.shape], {});IR for AS/NZS 1100
HWM-Water Ltd
HWM-Water Ltd
HWM-Water Ltd
HWM-Water Ltd