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.
drawing a line with Features Script
hervé_pipon
Member Posts: 60 ✭✭
Hello I want to draw a sphere and its radius.
I wrote this code:
The FeatureScript Notice shows :
for the line
I don't know why ??
Can somebody explain me ?
I also saw that sphereCenter has unit { "meter" : 2 } , but sphereRadius has unit { "meter" : 1 } ??
I don't understand
https://cad.onshape.com/documents/b5eb7563c9f8d34013c45353/w/a8328a22ba0852fc6de829ba/e/3946d40111b0b87308a02466
I wrote this code:
annotation { "Feature Type Name" : "drawSphere" } export const myFeature = defineFeature(function(context is Context, id is Id, definition is map) precondition { // Define the parameters of the feature type annotation { "Name" : "Radius" } isLength(definition.Radius, NONNEGATIVE_LENGTH_BOUNDS); annotation { "X" : "X coord" } isLength(definition.Xcoord, LENGTH_BOUNDS); annotation { "Y" : "X coord" } isLength(definition.Ycoord, LENGTH_BOUNDS); annotation { "Z" : "Z coord" } isLength(definition.Zcoord, LENGTH_BOUNDS); } { println("start"); var R = definition.Radius; var x0 = definition.Xcoord; var y0 = definition.Ycoord; var z0 = definition.Zcoord; println("R = " ~ R); println("x0 = " ~ x0); // Créez une esquisse 2D sur un plan de votre choix var sketch = newSketch(context, id + "sketch", { "sketchPlane" : qCreatedBy(makeId("Top"), EntityType.FACE) }); const sphereCenter = vector(x0, y0, z0) * meter; // Centre de la sphère const sphereRadius = vector(R.value, 0 , 0 ) * meter ; println("sphereCenter = " ~ sphereCenter); println("sphereRadius = " ~ sphereRadius); // Ajoutez une ligne à l'esquisse const startPoint = sphereCenter ; // Point de départ de la ligne Can not add ValueWithUnits (map) and number const endPoint = sphereCenter + sphereRadius; // Point d'arrêt de la ligne skLineSegment(sketch, "myLine", { "start" : startPoint, "end" : endPoint }); opSphere(context, id, { "radius" : R, "center" : vector(definition.Xcoord, definition.Ycoord, definition.Zcoord) }); });
The FeatureScript Notice shows :
start R = ValueWithUnits : { "unit" : UnitSpec : { "meter" : 1 } , "value" : 0.1 } x0 = ValueWithUnits : { "unit" : UnitSpec : { "meter" : 1 } , "value" : 0 } sphereCenter = Vector : [ ValueWithUnits : { "unit" : UnitSpec : { "meter" : 2 } , "value" : 0 } , ValueWithUnits : { "unit" : UnitSpec : { "meter" : 2 } , "value" : 0 } , ValueWithUnits : { "unit" : UnitSpec : { "meter" : 2 } , "value" : 0 } ] sphereRadius = Vector : [ ValueWithUnits : { "unit" : UnitSpec : { "meter" : 1 } , "value" : 0.1 } , ValueWithUnits : { "unit" : UnitSpec : { "meter" : 1 } , "value" : 0 } , ValueWithUnits : { "unit" : UnitSpec : { "meter" : 1 } , "value" : 0 } ] Result: Regeneration complete
with an error :
Precondition of operator+ failed (lhs.unit == rhs.unit) |
for the line
const endPoint = sphereCenter + sphereRadius; // Point d'arrêt de la ligne
I don't know why ??
Can somebody explain me ?
I also saw that sphereCenter has unit { "meter" : 2 } , but sphereRadius has unit { "meter" : 1 } ??
I don't understand
https://cad.onshape.com/documents/b5eb7563c9f8d34013c45353/w/a8328a22ba0852fc6de829ba/e/3946d40111b0b87308a02466
Tagged:
0
Comments
The "is" keyword is reserved for typechecks, so it's actually checking to see if startPoint passes a typecheck on type "undefined." You want to use the equality/inequality operators instead when checking inside of the precondition and for the println() checks.
Ignore that (explained in comment below)
Your 2D point check will still fail though. So you will want to make sure you are creating a 2D vector instead of a 3D one. Since you are using the top plane, you can just remove the Z inputs from your points and it shouldn't throw any more errors for you.
Also don't forget to solve your sketch when you're done, otherwise you won't see the line that you just programmed.
chadstoltzfus@premiercb.com
Concerning value.start != undefined; and value.start is undefined ; I don't really understand the use :
In the skLineSegment the check is on value.start is undefined || is2dPoint(value.start); what's the use ?
So the precondition is checking to make sure all boolean statements evaluate to true in order to execute the next block of code. So in skLineSegment the line is
value.start is undefined || is2dPoint(value.start);
This is checking to make sure value.start is a defined value and that it is also a 2D point. And since is2DPoint evaluates to true the precondition does not fail.However, if you separate them out,
value.start is undefined;
will evaluate to false (since value.start is defined). So that will cause the precondition to fail.I'm assuming that Onshape's function is checking if the field is undefined so that way the error is thrown by skLineSegment instead of in is2dPoint, as sending an undefined argument to is2dPoint would have the error thrown in that function and maybe return a more ambiguous error.
chadstoltzfus@premiercb.com