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.
Boolean add
guillaume_keller
Member Posts: 5 EDU
Hello everyone,
I want to create a feature script to create simple joint automatically,
I want to "glue" a few parts together but I don't know how to identify the loop-created entities and how to use them in the op boolean
There is also something about the deleted sketch that I can't suppress
If someone has an answer I'll be thankfull, I'm new in the FeatureScript
Here is the code :
FeatureScript 765;
import(path : "onshape/std/geometry.fs", version : "765.0");
annotation { "Feature Type Name" : "My Creneaux GOOD" }
export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
precondition
{
annotation { "Name" : "Face a couper", "Filter" : EntityType.FACE, "MaxNumberOfPicks" : 1 }
definition.face is Query;
annotation { "Name" : "Premier de la longueur ?" }
definition.premLon is boolean;
annotation { "Name" : "Epaisseur" }
definition.ep is string;
annotation { "Name" : "Longueur" }
definition.long is string;
annotation { "Name" : "Largeur" }
definition.larg is string;
}
{
var epaisseur = stringToNumber(definition.ep);
var creneau = 3 * epaisseur;
var longueur = stringToNumber(definition.long);
var largeur = stringToNumber(definition.larg);
//Premier support
var sketch1 = newSketch(context, id + "sketch1", {
"sketchPlane" : /*definition.face*/ qCreatedBy(makeId("Top"), EntityType.FACE)
});
skRectangle(sketch1, "rectangle1", {
"firstCorner" : vector(0, 0) * centimeter,
"secondCorner" : vector(longueur, largeur) * centimeter
});
skSolve(sketch1);
//Deuxieme support
var sketch2 = newSketch(context, id + "sketch2", {
"sketchPlane" : /*definition.face*/ qCreatedBy(makeId("Top"), EntityType.FACE)
});
skRectangle(sketch2, "rectangle2", {
"firstCorner" : vector(0, 0) * centimeter,
"secondCorner" : vector(longueur, largeur) * centimeter
});
skSolve(sketch2);
/*var sketch2 = newSketch(context, id + "sketch2", {
"sketchPlane" : qCreatedBy(makeId("Top"), EntityType.FACE)
});
skRectangle(sketch2, "rectangle3", {
"firstCorner" : vector(0, 0) * centimeter,
"secondCorner" : vector(longueur,largeur) * centimeter
});
skRectangle(sketch2, "rectangle2", {
"firstCorner" : vector(-creneau, -creneau) * centimeter,
"secondCorner" : vector(longueur+creneau,largeur+creneau) * centimeter
});
skSolve(sketch2);*/
//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//Premiere partie
//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//Lignes sur X
for (var i = 0; i < longueur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid1" + i, {
"corner1" : vector(i * 2 * creneau, 0, 0) * centimeter,
"corner2" : vector(i * 2 * creneau + creneau, epaisseur, epaisseur) * centimeter
});
}
for (var i = 0; i < longueur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid2" + i, {
"corner1" : vector(i * 2 * creneau, 0 + largeur - epaisseur, 0) * centimeter,
"corner2" : vector(i * 2 * creneau + creneau, epaisseur + largeur - epaisseur, epaisseur) * centimeter
});
}
//Lignes sur Y
for (var i = 0; i < largeur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid3" + i, {
"corner1" : vector(0, i * 2 * creneau, 0) * centimeter,
"corner2" : vector(epaisseur, i * 2 * creneau + creneau, epaisseur) * centimeter
});
}
for (var i = 0; i < largeur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid4" + i, {
"corner1" : vector(0 + longueur - epaisseur, i * 2 * creneau, 0) * centimeter,
"corner2" : vector(epaisseur + longueur - epaisseur, i * 2 * creneau + creneau, epaisseur) * centimeter
});
}
extrude(context, id + "extrude1", {
"entities" : qSketchRegion(id + "sketch1"),
"endBound" : BoundingType.BLIND,
"depth" : -0.01 * centimeter,
"operationType" : NewBodyOperationType.ADD
});
extrude(context, id + "extrude2", {
"entities" : qSketchRegion(id + "sketch2"),
"endBound" : BoundingType.BLIND,
"depth" : (0.01 + epaisseur * 3) * centimeter,
"operationType" : NewBodyOperationType.NEW,
});
extrude(context, id + "extrude3", {
"entities" : qSketchRegion(id + "sketch2"),
"endBound" : BoundingType.BLIND,
"depth" : (epaisseur * 3) * centimeter,
"operationType" : NewBodyOperationType.NEW,
});
opBoolean(context, id + "boolean1", {
"tools" : qCreatedBy(id + "extrude3", EntityType.BODY),
"targets" : qCreatedBy(id + "extrude2", EntityType.BODY),
"operationType" : BooleanOperationType.SUBTRACTION
});
//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//Deuxieme partie
//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
for (var i = 0; i < longueur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid5" + i, {
"corner1" : vector(creneau + i * 2 * creneau, 0, 2 * epaisseur) * centimeter,
"corner2" : vector(creneau + i * 2 * creneau + creneau, epaisseur, 2 * epaisseur + epaisseur) * centimeter
});
}
/*for (var i = 0; i < longueur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid6" + i, {
"corner1" : vector(creneau + i * 2 * creneau, 0 + largeur - epaisseur, 2 * epaisseur) * centimeter,
"corner2" : vector(creneau + i * 2 * creneau + creneau, epaisseur + largeur - epaisseur, 2 * epaisseur + epaisseur) * centimeter
});
}*/
//Lignes sur Y
for (var i = 0; i < largeur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid7" + i, {
"corner1" : vector(0, creneau + i * 2 * creneau, 2 * epaisseur) * centimeter,
"corner2" : vector(epaisseur, creneau + i * 2 * creneau + creneau, 2 * epaisseur + epaisseur) * centimeter
});
}
});
I want to create a feature script to create simple joint automatically,
I want to "glue" a few parts together but I don't know how to identify the loop-created entities and how to use them in the op boolean
There is also something about the deleted sketch that I can't suppress
If someone has an answer I'll be thankfull, I'm new in the FeatureScript
Here is the code :
FeatureScript 765;
import(path : "onshape/std/geometry.fs", version : "765.0");
annotation { "Feature Type Name" : "My Creneaux GOOD" }
export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
precondition
{
annotation { "Name" : "Face a couper", "Filter" : EntityType.FACE, "MaxNumberOfPicks" : 1 }
definition.face is Query;
annotation { "Name" : "Premier de la longueur ?" }
definition.premLon is boolean;
annotation { "Name" : "Epaisseur" }
definition.ep is string;
annotation { "Name" : "Longueur" }
definition.long is string;
annotation { "Name" : "Largeur" }
definition.larg is string;
}
{
var epaisseur = stringToNumber(definition.ep);
var creneau = 3 * epaisseur;
var longueur = stringToNumber(definition.long);
var largeur = stringToNumber(definition.larg);
//Premier support
var sketch1 = newSketch(context, id + "sketch1", {
"sketchPlane" : /*definition.face*/ qCreatedBy(makeId("Top"), EntityType.FACE)
});
skRectangle(sketch1, "rectangle1", {
"firstCorner" : vector(0, 0) * centimeter,
"secondCorner" : vector(longueur, largeur) * centimeter
});
skSolve(sketch1);
//Deuxieme support
var sketch2 = newSketch(context, id + "sketch2", {
"sketchPlane" : /*definition.face*/ qCreatedBy(makeId("Top"), EntityType.FACE)
});
skRectangle(sketch2, "rectangle2", {
"firstCorner" : vector(0, 0) * centimeter,
"secondCorner" : vector(longueur, largeur) * centimeter
});
skSolve(sketch2);
/*var sketch2 = newSketch(context, id + "sketch2", {
"sketchPlane" : qCreatedBy(makeId("Top"), EntityType.FACE)
});
skRectangle(sketch2, "rectangle3", {
"firstCorner" : vector(0, 0) * centimeter,
"secondCorner" : vector(longueur,largeur) * centimeter
});
skRectangle(sketch2, "rectangle2", {
"firstCorner" : vector(-creneau, -creneau) * centimeter,
"secondCorner" : vector(longueur+creneau,largeur+creneau) * centimeter
});
skSolve(sketch2);*/
//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//Premiere partie
//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//Lignes sur X
for (var i = 0; i < longueur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid1" + i, {
"corner1" : vector(i * 2 * creneau, 0, 0) * centimeter,
"corner2" : vector(i * 2 * creneau + creneau, epaisseur, epaisseur) * centimeter
});
}
for (var i = 0; i < longueur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid2" + i, {
"corner1" : vector(i * 2 * creneau, 0 + largeur - epaisseur, 0) * centimeter,
"corner2" : vector(i * 2 * creneau + creneau, epaisseur + largeur - epaisseur, epaisseur) * centimeter
});
}
//Lignes sur Y
for (var i = 0; i < largeur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid3" + i, {
"corner1" : vector(0, i * 2 * creneau, 0) * centimeter,
"corner2" : vector(epaisseur, i * 2 * creneau + creneau, epaisseur) * centimeter
});
}
for (var i = 0; i < largeur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid4" + i, {
"corner1" : vector(0 + longueur - epaisseur, i * 2 * creneau, 0) * centimeter,
"corner2" : vector(epaisseur + longueur - epaisseur, i * 2 * creneau + creneau, epaisseur) * centimeter
});
}
extrude(context, id + "extrude1", {
"entities" : qSketchRegion(id + "sketch1"),
"endBound" : BoundingType.BLIND,
"depth" : -0.01 * centimeter,
"operationType" : NewBodyOperationType.ADD
});
extrude(context, id + "extrude2", {
"entities" : qSketchRegion(id + "sketch2"),
"endBound" : BoundingType.BLIND,
"depth" : (0.01 + epaisseur * 3) * centimeter,
"operationType" : NewBodyOperationType.NEW,
});
extrude(context, id + "extrude3", {
"entities" : qSketchRegion(id + "sketch2"),
"endBound" : BoundingType.BLIND,
"depth" : (epaisseur * 3) * centimeter,
"operationType" : NewBodyOperationType.NEW,
});
opBoolean(context, id + "boolean1", {
"tools" : qCreatedBy(id + "extrude3", EntityType.BODY),
"targets" : qCreatedBy(id + "extrude2", EntityType.BODY),
"operationType" : BooleanOperationType.SUBTRACTION
});
//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//Deuxieme partie
//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
for (var i = 0; i < longueur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid5" + i, {
"corner1" : vector(creneau + i * 2 * creneau, 0, 2 * epaisseur) * centimeter,
"corner2" : vector(creneau + i * 2 * creneau + creneau, epaisseur, 2 * epaisseur + epaisseur) * centimeter
});
}
/*for (var i = 0; i < longueur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid6" + i, {
"corner1" : vector(creneau + i * 2 * creneau, 0 + largeur - epaisseur, 2 * epaisseur) * centimeter,
"corner2" : vector(creneau + i * 2 * creneau + creneau, epaisseur + largeur - epaisseur, 2 * epaisseur + epaisseur) * centimeter
});
}*/
//Lignes sur Y
for (var i = 0; i < largeur / 2 / creneau; i += 1)
{
fCuboid(context, id + "cuboid7" + i, {
"corner1" : vector(0, creneau + i * 2 * creneau, 2 * epaisseur) * centimeter,
"corner2" : vector(epaisseur, creneau + i * 2 * creneau + creneau, 2 * epaisseur + epaisseur) * centimeter
});
}
});
Tagged:
0
Comments
To answer your first question, you need to use qUnion. A query for all the loop created cuboids as you have the code now would look like:
The reason that you don't have to put all the individual "+ i" parts for the ids is because ids are hierarchical, so a parent id also represents the operations of its children. Using this knowledge, you could simplify your code by having your code look like:
More reading on id hierarchy here:
https://cad.onshape.com/FsDoc/library.html#Id
Another simple way to collect these entities without changing how your code currently looks would be to do this before your first for loop:
And then after each for loop:
With <index> replaced with the corresponding number in that for loop.
Hope this helps!
My script is now running well !
If I can ask another little question, I want to catch the length and the width of a rectangular face, is there a function to do this ? or do I need to catch them by hand ?
Regards,
Guillaume
Another little question, if you want to get a point coordinates, what is the function related to ?
All of the geometry evaluation functions are found in `evaluate.fs` and are documented here:
https://cad.onshape.com/FsDoc/library.html#module-evaluate.fs