Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. 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.

Options

How to copy user sketch to another plane and extrude (Fixed)

sean_connor714sean_connor714 Member Posts: 6
edited November 2023 in FeatureScript
Hey all,

I have a predefined user sketch on a default plane, and I'd like to essentially copy that user sketch to a feature script generated plane and extrude it from there.

Is something like that possible through feature script?

FeatureScript 2180;
import(path : "onshape/std/common.fs", version : "2180.0");

annotation { "Feature Type Name" : "Truss" }
export const truss = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        annotation { "Name" : "Slot Sketch", "Filter" : EntityType.FACE, }
        definition.slot is Query;
        
        annotation { "Name" : "Slot Wall Sketch", "Filter" : EntityType.FACE, }
        definition.slotWall is Query;
        
        annotation { "Name" : "Body Radius" }
        isLength(definition.bodyRadius, LENGTH_BOUNDS);
    }
    {
        opSphere(context, id + "sphere1", {
                "center" : vector(0, 0, 0) * millimeter,
                "radius" : definition.bodyRadius
        });
        
        var plane = createPlaneOnSphere(context, id + "pointPlane1", vector(0, 0, 0), definition.bodyRadius, vector(1, 0.5) * radian);

        opExtrude(context, id + "slot", {
                "entities" : definition.slotWall,
                // Here I'd like to essentially copy the sketch the calculated plane above and extrude from there instead.                
                "direction" : evOwnerSketchPlane(context, {"entity" : definition.slotWall}).normal,
                "endBound" : BoundingType.BLIND,
                "endDepth" : definition.bodyRadius * 2
        });
    });

export function createPlaneOnSphere(context is Context, id is Id, sphereCenter is Vector, sphereRadius is ValueWithUnits, uv is Vector)
{
    // Convert U & V (spherical coordinates) to Cartesian coordinates
    println(uv[1]);
    var x = sphereCenter[0] + sphereRadius.value * sin(uv[1]) * cos(uv[0]);
    var y = sphereCenter[1] + sphereRadius.value * sin(uv[1]) * sin(uv[0]);
    var z = sphereCenter[2] + sphereRadius.value * cos(uv[1]);

    var pointOnSphere = vector(x, y, z);

    // The normal vector is simply the vector from the center to the point on the sphere
    var normalVector = normalize(pointOnSphere - sphereCenter);

    println(pointOnSphere);
    // Create the plane
    var plane = opPlane(context, id + "plane1", {
            "plane" : plane(pointOnSphere, normalVector)
    });

    return plane;
}}

Thanks,

Comments

  • Options
    NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,417
    No need to move the sketch - create the extrude, transform, boolean
    Senior Director, Technical Services, EMEAI
  • Options
    sean_connor714sean_connor714 Member Posts: 6
    Yep, got it, thanks. Starting to understand feature script more:

    FeatureScript 2180;
    import(path : "onshape/std/common.fs", version : "2180.0");
    
    annotation { "Feature Type Name" : "Truss" }
    export const truss = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Slot", "Filter" : EntityType.FACE, }
            definition.slot is Query;
            
            annotation { "Name" : "Slot Interior", "Filter" : EntityType.FACE, }
            definition.slotInterior is Query;
            
            annotation { "Name" : "Body Radius" }
            isLength(definition.bodyRadius, LENGTH_BOUNDS);
        }
        {
            opSphere(context, id + "sphere", {
                    "center" : vector(0, 0, 0) * millimeter,
                    "radius" : definition.bodyRadius
            });
            
            for (var i = 0; i < 4; i += 1)
            {
                println(i);
                var plane = createPlaneOnSphere(context, vector(0, 0, 0), definition.bodyRadius, vector(i / 2.0 * PI, PI * 0.75) * radian);
            
                opExtrude(context, id + i + "slot", {
                        "entities" : definition.slot,
                        "direction" : evOwnerSketchPlane(context, {"entity" : definition.slot}).normal,
                        "endBound" : BoundingType.BLIND,
                        "endDepth" : definition.bodyRadius * 2
                });
                
                opExtrude(context, id + i + "slotInterior", {
                        "entities" : definition.slotInterior,
                        "direction" : evOwnerSketchPlane(context, {"entity" : definition.slotInterior}).normal,
                        "endBound" : BoundingType.BLIND,
                        "endDepth" : definition.bodyRadius * 2,
                        "startDepth" : definition.bodyRadius * -1,
                        "startBound" : BoundingType.BLIND
                });
                
                opTransform(context, id + i + "transform", {
                        "bodies" : qUnion([
                            qCreatedBy(id + i + "slot", EntityType.BODY), 
                            qCreatedBy(id + i + "slotInterior", EntityType.BODY)
                        ]),
                        "transform" : transform(evOwnerSketchPlane(context, {"entity" : definition.slot}), plane)
                });
                
                opBoolean(context, id + i + "union", {
                     "tools" : qUnion([
                         qCreatedBy(id + "sphere", EntityType.BODY),
                         qCreatedBy(id + i + "slot", EntityType.BODY)
                    ]),
                     "operationType" : BooleanOperationType.UNION
                });
                
                opBoolean(context, id + i + "subtract", {
                        "tools" : qCreatedBy(id + i + "slotInterior", EntityType.BODY),
                        "targets" : qCreatedBy(id + "sphere", EntityType.BODY),
                        "operationType" : BooleanOperationType.SUBTRACTION
                });
            }
        });
    
    export function createPlaneOnSphere(context is Context, sphereCenter is Vector, sphereRadius is ValueWithUnits, uv is Vector)
    {
        // Convert U & V (spherical coordinates) to Cartesian coordinates
        println(uv[1]);
        var x = sphereCenter[0] + sphereRadius.value * sin(uv[1]) * cos(uv[0]);
        var y = sphereCenter[1] + sphereRadius.value * sin(uv[1]) * sin(uv[0]);
        var z = sphereCenter[2] + sphereRadius.value * cos(uv[1]);
    
        var pointOnSphere = vector(x, y, z);
    
        // The normal vector is simply the vector from the center to the point on the sphere
        var normalVector = normalize(pointOnSphere - sphereCenter);
    
        println(pointOnSphere);
        
        var newPlane = plane(pointOnSphere * millimeter, normalVector);
        //var newPlane = opPlane(context, id + "plane1", {
        //        "plane" : plane(pointOnSphere * millimeter, normalVector)
        //});
        
        println(newPlane);
        
        return newPlane;
    }

  • Options
    sean_connor714sean_connor714 Member Posts: 6
    Yep, got it, thanks. Starting to understand feature script more:

    FeatureScript 2180;
    import(path : "onshape/std/common.fs", version : "2180.0");
    
    annotation { "Feature Type Name" : "Truss" }
    export const truss = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Slot", "Filter" : EntityType.FACE, }
            definition.slot is Query;
            
            annotation { "Name" : "Slot Interior", "Filter" : EntityType.FACE, }
            definition.slotInterior is Query;
            
            annotation { "Name" : "Body Radius" }
            isLength(definition.bodyRadius, LENGTH_BOUNDS);
        }
        {
            opSphere(context, id + "sphere", {
                    "center" : vector(0, 0, 0) * millimeter,
                    "radius" : definition.bodyRadius
            });
            
            for (var i = 0; i < 4; i += 1)
            {
                println(i);
                var plane = createPlaneOnSphere(context, vector(0, 0, 0), definition.bodyRadius, vector(i / 2.0 * PI, PI * 0.75) * radian);
            
                opExtrude(context, id + i + "slot", {
                        "entities" : definition.slot,
                        "direction" : evOwnerSketchPlane(context, {"entity" : definition.slot}).normal,
                        "endBound" : BoundingType.BLIND,
                        "endDepth" : definition.bodyRadius * 2
                });
                
                opExtrude(context, id + i + "slotInterior", {
                        "entities" : definition.slotInterior,
                        "direction" : evOwnerSketchPlane(context, {"entity" : definition.slotInterior}).normal,
                        "endBound" : BoundingType.BLIND,
                        "endDepth" : definition.bodyRadius * 2,
                        "startDepth" : definition.bodyRadius * -1,
                        "startBound" : BoundingType.BLIND
                });
                
                opTransform(context, id + i + "transform", {
                        "bodies" : qUnion([
                            qCreatedBy(id + i + "slot", EntityType.BODY), 
                            qCreatedBy(id + i + "slotInterior", EntityType.BODY)
                        ]),
                        "transform" : transform(evOwnerSketchPlane(context, {"entity" : definition.slot}), plane)
                });
                
                opBoolean(context, id + i + "union", {
                     "tools" : qUnion([
                         qCreatedBy(id + "sphere", EntityType.BODY),
                         qCreatedBy(id + i + "slot", EntityType.BODY)
                    ]),
                     "operationType" : BooleanOperationType.UNION
                });
                
                opBoolean(context, id + i + "subtract", {
                        "tools" : qCreatedBy(id + i + "slotInterior", EntityType.BODY),
                        "targets" : qCreatedBy(id + "sphere", EntityType.BODY),
                        "operationType" : BooleanOperationType.SUBTRACTION
                });
            }
        });
    
    export function createPlaneOnSphere(context is Context, sphereCenter is Vector, sphereRadius is ValueWithUnits, uv is Vector)
    {
        // Convert U & V (spherical coordinates) to Cartesian coordinates
        println(uv[1]);
        var x = sphereCenter[0] + sphereRadius.value * sin(uv[1]) * cos(uv[0]);
        var y = sphereCenter[1] + sphereRadius.value * sin(uv[1]) * sin(uv[0]);
        var z = sphereCenter[2] + sphereRadius.value * cos(uv[1]);
    
        var pointOnSphere = vector(x, y, z);
    
        // The normal vector is simply the vector from the center to the point on the sphere
        var normalVector = normalize(pointOnSphere - sphereCenter);
    
        println(pointOnSphere);
        
        var newPlane = plane(pointOnSphere * millimeter, normalVector);
        //var newPlane = opPlane(context, id + "plane1", {
        //        "plane" : plane(pointOnSphere * millimeter, normalVector)
        //});
        
        println(newPlane);
        
        return newPlane;
    }
Sign In or Register to comment.