FeatureScript 2780; import(path : "onshape/std/geometry.fs", version : "2780.0"); //import(path : "onshape/std/common.fs", version : "2780.0"); //import(path : "onshape/std/feature.fs", version : "2780.0"); annotation { "Feature Type Name" : "Create Half Joiners", "Editing Logic Function" : "setDefaults2", "Filter Selector" : "fs" } export const HalfJoinerEdge = defineFeature(function(context is Context, id is Id, definition is map) precondition { annotation { "Name" : "Select edge to join", "Filter" : (EntityType.EDGE && GeometryType.LINE && ConstructionObject.NO && SketchObject.NO), "MaxNumberOfPicks" : 1} definition.edge is Query; annotation { "Name" : "Joiner width", "Default" : 10 * millimeter, "UIHint" : "REMEMBER_PREVIOUS_VALUE"} isLength(definition.width, NONNEGATIVE_LENGTH_BOUNDS); annotation { "Name" : "Joiner length", "Default" : 20 * millimeter, "UIHint" : "REMEMBER_PREVIOUS_VALUE" } isLength(definition.length, NONNEGATIVE_LENGTH_BOUNDS); } { var w = definition.width; var l = definition.length; const maleBody = qOwnerBody(definition.edge); const femaleBody = qSubtraction(qContainsPoint(qBodyType(qEverything(EntityType.BODY), BodyType.SOLID), evEdgeTangentLine(context, { "edge" : definition.edge, "parameter" : 0.5 }).origin), maleBody); if (evaluateQuery(context, femaleBody) == []) { throw regenError("Selected edge is not adjacent to another body", ["edge"]); } const jointFaces = qAdjacent(definition.edge, AdjacencyType.EDGE, EntityType.FACE); // Only possible to have two faces for one edge so find the area for each face const faceAreas = [evArea(context, { "entities" : qNthElement(jointFaces, 0) }), evArea(context, { "entities" : qNthElement(jointFaces, 1) })]; var faceIndex = 0; if (faceAreas[0] > faceAreas[1]) // Find which face is the smallest and make it the male side { faceIndex = 1; } var maleFace = evFaceTangentPlane(context, { "face" : qNthElement(jointFaces, faceIndex), "parameter" : vector(0.5, 0.5) }); const femaleFace = qContainsPoint(qOwnedByBody(femaleBody, EntityType.FACE), maleFace.origin); if (evArea(context, { "entities" : femaleFace }) < faceAreas[faceIndex]) { throw regenError("Selected edge belongs to the wrong body", ["edge"]); } //*********************************************** // ADD ITEMS IN THE PLACE PERPENDICULAR TO THE FACE DIRECTION //*********************************************** const edgeGeometry = evEdgeTangentLine(context, { "edge" : definition.edge, "parameter" : 0.5 }); // 0.5 = midpoint of the edge const edgeDirection = normalize(edgeGeometry.direction); const perpPlane = plane(maleFace.origin, edgeDirection, maleFace.normal); var skJoiner_base = newSketchOnPlane(context, id + "skJoiner_base", { "sketchPlane" : perpPlane }); const p1 = vector(-w, l/2); // Top left const p2 = vector(0 * millimeter, l/2); // Top right const p3 = vector(-l/3 * tan(30 * degree), l/6); // Upper right triangle const p4 = vector(-l/3 * tan(30 * degree), -l/6); // Upper right triangle const p5 = vector(0 * millimeter, -l/2); // Bottom right corner const p6 = vector(-w, -l/2); // Bottom left const p7 = vector(l/3 * tan(30 * degree), -l/6); // Upper right triangle const p8 = vector(l/3 * tan(30 * degree), l/6); // Upper right triangle skPolyline(skJoiner_base, "profile1", { "points" : [p1, p2, p3, p4, p5, p6, p1], "closed" : true }); skSolve(skJoiner_base); opExtrude(context, id + "joiner_base", { "entities" : qSketchRegion(id + "skJoiner_base"), "direction" : perpPlane.normal, "endBound" : BoundingType.BLIND, "endDepth" : w / 2, "startBound" : BoundingType.BLIND, "startDepth" : w / 2 }); var skJoiner_top = newSketchOnPlane(context, id + "skJoiner_top", { "sketchPlane" : perpPlane }); skPolyline(skJoiner_top, "profile1", { "points" : [p2, p3, p4, p5, p7, p8, p2], "closed" : true }); skSolve(skJoiner_top); opExtrude(context, id + "joiner_top", { "entities" : qSketchRegion(id + "skJoiner_top"), "direction" : perpPlane.normal, "endBound" : BoundingType.BLIND, "endDepth" : w / 6, "startBound" : BoundingType.BLIND, "startDepth" : w / 6 }); opExtrude(context, id + "joiner_clear", { "entities" : qSketchRegion(id + "skJoiner_top"), "direction" : perpPlane.normal, "endBound" : BoundingType.BLIND, "endDepth" : w / 2, "startBound" : BoundingType.BLIND, "startDepth" : w / 2 }); /* // Add the cut tool to the female body const clearTool = qCreatedBy(id + "joiner_clear", EntityType.BODY); opBoolean(context, id + "union_female", { "targets" : femaleBody, "tools" : clearTool, "operationType" : BooleanOperationType.UNION }); */ /* //*********************************************** // Subtract joiner_clear from the MALE body and merge to the FEMALE //*********************************************** const clearTool = qCreatedBy(id + "joiner_clear", EntityType.BODY); opBoolean(context, id + "cut_male", { "targets" : femaleBody, "tools" : clearTool, "operationType" : BooleanOperationType.SUBTRACTION, "keepTools" : true }); */ /* const clearTool = qCreatedBy(id + "joiner_clear", EntityType.BODY); // Union with female FIRST opBoolean(context, id + "union_female", { "targets" : qOwnerBody(qNthElement(jointFaces, 1)), "tools" : clearTool, "operationType" : BooleanOperationType.UNION }); // Then subtract from male opBoolean(context, id + "cut_male", { "targets" : maleBody, "tools" : clearTool, "operationType" : BooleanOperationType.SUBTRACTION, "keepTools" : true }); */ /* //*********************************************** // UNION JOINER PARTS TOGETHER //*********************************************** opBoolean(context, id + "union_female", { "targets" : femaleBody, "tools" : clearTool, "operationType" : BooleanOperationType.UNION }); */ /* opBoolean(context, id + "u_all", { "targets" : femaleBody, "tools" : qUnion([ qCreatedBy(id + "joiner_clear", EntityType.BODY)]), "operationType" : BooleanOperationType.UNION }); */ /* // Force evaluation of femaleBody query const femaleBodies = evaluateQuery(context, femaleBody); if (size(femaleBodies) > 0) { opBoolean(context, id + "union_female", { "targets" : femaleBody, "tools" : clearTool, "operationType" : BooleanOperationType.UNION }); } */ /* const clearTool = qCreatedBy(id + "joiner_clear", EntityType.BODY); opBoolean(context, id + "cut_male", { "targets" : maleBody, "tools" : clearTool, "operationType" : BooleanOperationType.SUBTRACTION, "keepTools" : true }); // Get all solid bodies that aren't maleBody or clearTool const allBodies = qBodyType(qEverything(EntityType.BODY), BodyType.SOLID); const remainingBodies = qSubtraction(allBodies, qUnion([maleBody, clearTool])); opBoolean(context, id + "union_female", { "targets" : remainingBodies, "tools" : clearTool, "operationType" : BooleanOperationType.UNION }); */ /* const clearTool = qCreatedBy(id + "joiner_clear", EntityType.BODY); const femaleBodyQuery = qOwnerBody(femaleFace); // Test intersection to see if they overlap opBoolean(context, id + "test_intersect", { "targets" : femaleBodyQuery, "tools" : clearTool, "operationType" : BooleanOperationType.INTERSECTION }); */ /* const clearTool = qCreatedBy(id + "joiner_clear", EntityType.BODY); // Get the bodies that own the faces on the edge const body1 = qOwnerBody(qNthElement(jointFaces, 0)); const body2 = qOwnerBody(qNthElement(jointFaces, 1)); // One of these is femaleBody - try unioning with both separately try { opBoolean(context, id + "union_test1", { "targets" : body1, "tools" : clearTool, "operationType" : BooleanOperationType.UNION }); } catch (err) { // If body1 fails, try body2 opBoolean(context, id + "union_test2", { "targets" : body2, "tools" : clearTool, "operationType" : BooleanOperationType.UNION }); } */ /* const clearTool = qCreatedBy(id + "joiner_clear", EntityType.BODY); // Debug - print what clearTool resolves to var toolBodies = evaluateQuery(context, clearTool); println("clearTool count: " ~ size(toolBodies)); if (size(toolBodies) == 0) { throw regenError("joiner_clear created no bodies", ["joiner_clear"]); } */ /* // --- ensure joiner_clear was created as a NEW body (do this in its opExtrude) --- // opExtrude(context, id + "joiner_clear", { ..., "operationType" : NewBodyOperation.NEW }); // --- robust union of joiner_clear into the FEMALE body --- const solids = qBodyType(qEverything(EntityType.BODY), BodyType.SOLID); const clearTool = qIntersection([ qCreatedBy(id + "joiner_clear", EntityType.BODY), solids ]); // Resolve the female target using the face you already computed (most reliable) const femaleSolid = qIntersection([ qOwnerBody(femaleFace), solids ]); if (evaluateQuery(context, clearTool) == []) throw regenError("joiner_clear is not a solid at union time", []); if (evaluateQuery(context, femaleSolid) == []) throw regenError("female body not found at union time", []); // Option A: direct union (usually fine) opBoolean(context, id + "union_female", { "targets" : femaleSolid, "tools" : clearTool, "operationType" : BooleanOperationType.UNION }); */ /* // Option B (use if A throws BOOLEAN_BAD_INPUT): duplicate the tool and union the copy // Resolve the female target using the face you already computed (most reliable) const solids = qBodyType(qEverything(EntityType.BODY), BodyType.SOLID); const clearTool = qIntersection([ qCreatedBy(id + "joiner_clear", EntityType.BODY), solids ]); const femaleSolid = qIntersection([ qOwnerBody(femaleFace), solids ]); opPattern(context, id + "dup_clear_for_union", { "entities" : clearTool, "transforms" : [ transform(vector(0,0,0) * meter) ], // identity = exact copy "instanceNames" : [ "copy" ] }); const clearCopy = qIntersection([ qCreatedBy(id + "dup_clear_for_union", EntityType.BODY), solids ]); opBoolean(context, id + "union_female_copy", { "targets" : femaleSolid, "tools" : clearCopy, "operationType" : BooleanOperationType.UNION }); // optional cleanup (the copy is consumed by union anyway) */ /* // Resolve solids right now const solids = qBodyType(qEverything(EntityType.BODY), BodyType.SOLID); const clearTool = qIntersection([ qCreatedBy(id + "joiner_clear", EntityType.BODY), solids ]); const femaleSolid = qIntersection([ qOwnerBody(femaleFace), solids ]); // use the face you already computed // 1) Make the intersection as a NEW body (keep originals) opBoolean(context, id + "clear_in_female", { "targets" : femaleSolid, "tools" : clearTool, "operationType" : BooleanOperationType.INTERSECTION, "keepTools" : true }); // 2) Union the intersection back into the female const clearInsideFemale = qIntersection([ qCreatedBy(id + "clear_in_female", EntityType.BODY), solids ]); opBoolean(context, id + "union_female_from_intersection", { "targets" : femaleSolid, "tools" : clearInsideFemale, "operationType" : BooleanOperationType.UNION }); // (Optional) cleanup opDeleteBodies(context, id + "cleanup_intersection", { "entities" : clearInsideFemale }); */ /* const solids = qBodyType(qEverything(EntityType.BODY), BodyType.SOLID); const clearTool = qIntersection([ qCreatedBy(id + "joiner_clear", EntityType.BODY), solids ]); const femaleSolid = qIntersection([ qOwnerBody(femaleFace), solids ]); // Compute a tiny inward vector toward the female const boxTool = evBox3d(context, { "topology": clearTool }); const boxFemale = evBox3d(context, { "topology": femaleSolid }); var nudgeDir = normalize(boxFemale.center - boxTool.center); // points toward female if (!isFinite(nudgeDir[0])) nudgeDir = vector(1,0,0); // fallback // Nudge by a micron opTransform(context, id + "nudge_clear_into_female", { "bodies" : clearTool, "transform" : transform(nudgeDir * (1e-6 * meter)) }); // Now union opBoolean(context, id + "union_female_nudged", { "targets" : femaleSolid, "tools" : clearTool, "operationType" : BooleanOperationType.UNION }); */ /* // FIRST: Subtract from male (but keep the tool) const solids = qBodyType(qEverything(EntityType.BODY), BodyType.SOLID); const clearTool = qIntersection([ qCreatedBy(id + "joiner_clear", EntityType.BODY), solids ]); opBoolean(context, id + "cut_male", { "targets" : maleBody, "tools" : clearTool, "operationType" : BooleanOperationType.SUBTRACTION, "keepTools" : true // CRITICAL! }); // SECOND: Union what's left with female opBoolean(context, id + "union_female", { "targets" : femaleBody, "tools" : clearTool, "operationType" : BooleanOperationType.UNION }); */ //*********************************************** // ADD ITEMS IN THE NORMAL TO THE FACE DIRECTION //*********************************************** const normalPlane = plane(maleFace.origin, maleFace.normal, edgeDirection); var skCenter = newSketchOnPlane(context, id + "skCenter", { "sketchPlane" : normalPlane }); const c1 = vector(w/6, l/6); const c2 = vector(w/6 + w/8, 0 * millimeter); const c3 = vector(w/6, -l/6); const c4 = vector(-w/6, -l/6); const c5 = vector(-w/6 - w/8, 0 * millimeter); const c6 = vector(-w/6, l/6); skPolyline(skCenter, "profile1", { "points" : [c1, c2, c3, c4, c5, c6, c1], "closed" : true }); skSolve(skCenter); opExtrude(context, id + "joiner_center", { "entities" : qSketchRegion(id + "skCenter"), "direction" : normalPlane.normal, "endBound" : BoundingType.BLIND, "endDepth" : l/3 * tan(30 * degree), "startBound" : BoundingType.BLIND, "startDepth" : l/3 * tan(30 * degree) }); //*********************************************** // ADD ITEMS IN THE THIRD TO THE FACE DIRECTION //*********************************************** const perpPlanePos = plane(perpPlane.origin + (w/6 * perpPlane.normal), perpPlane.normal); var skSnaps = newSketchOnPlane(context, id + "skSnaps", { "sketchPlane" : perpPlanePos }); const s1 = vector(l * 3/5/2, -1 * millimeter); const s2 = vector(l * 3/5/2, 1 * millimeter); const s3 = vector(-l * 3/5/2, 1 * millimeter); const s4 = vector(-l * 3/5/2, -1 * millimeter); skPolyline(skSnaps, "profile1", { "points" : [s1, s2, s3, s4, s1], "closed" : true }); skSolve(skSnaps); // Extrude the snap features extrude(context, id + "snaps", { "entities" : qSketchRegion(id + "skSnaps"), "direction" : perpPlanePos.normal, "depth" : 0.5 * millimeter }); // Draft the snap features opDraft(context, id + "snaps_draft", { "draftType" : DraftType.NEUTRAL_PLANE, "draftFaces" : qNonCapEntity(id + "snaps", EntityType.FACE), "neutralPlane" : qSketchRegion(id + "skSnaps"), "pullVec" : perpPlanePos.normal, "angle" : (90-26.708) * degree }); // Mirror the snapes using opPattern - this keeps original AND creates mirrored copy opPattern(context, id + "mirror", { "entities" : qOwnerBody(qNonCapEntity(id + "snaps", EntityType.FACE)), "transforms" : [ transform(vector(0, 0, 0) * meter), // Identity transform (keeps original) mirrorAcross(perpPlane) // Mirror transform (creates copy) ], "instanceNames" : ["original", "mirrored"] // Optional: name the instances }); //*********************************************** // DELETE SKETCHES //*********************************************** // Delete the original since above creates two copies opDeleteBodies(context, id + "deleteBodies", { "entities" : qCreatedBy(id+"snaps", EntityType.BODY) }); // Delete multiple sketches at once opDeleteBodies(context, id + "deleteAllSketches", { "entities" : qUnion([ qCreatedBy(id + "skJoiner_base", EntityType.BODY), qCreatedBy(id + "skJoiner_top", EntityType.BODY), qCreatedBy(id + "skCenter", EntityType.BODY), qCreatedBy(id + "skSnaps", EntityType.BODY), // Add any other sketch IDs here ]) }); //*********************************************** // UNION JOINER PARTS TOGETHER //*********************************************** opBoolean(context, id + "u_all", { "targets" : maleBody, "tools" : qUnion([ qCreatedBy(id + "joiner_base", EntityType.BODY), qCreatedBy(id + "joiner_top", EntityType.BODY), qCreatedBy(id + "joiner_center", EntityType.BODY), qCreatedBy(id + "snaps", EntityType.BODY), qCreatedBy(id + "mirror", EntityType.BODY) ]), "operationType" : BooleanOperationType.UNION }); }); export function setDefaults2(context is Context, id is Id, oldDefinition is map, definition is map) returns map { // if(definition.endType == endType. return definition; }