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.

Custom feature script for 2 extrudes

KyleDarnellKyleDarnell Member Posts: 7 PRO
I would like to make a feature script that will make 2 extrudes. I do this exact same action very often and I would like to make this automated. Regarding FeatureScript I am still relatively new (I have made a few featurescipts for variable lists), so I wanted to ask what I would need to do to make these actions happen. Here is a link to a document with the task I will be wanting to make into a FeatureScript. It will be in the folder named "FEAURE SCRIPT?"

Thank you

Best Answer

  • MichaelPascoeMichaelPascoe Member Posts: 835 PRO
    edited February 24 Answer ✓
    @KyleDarnell Welcome to FeatureScript!
    Here are the general steps you will need to be able to do this:

    https://cad.onshape.com/documents/82e1c77948a53493dda93e14/w/903175619ca8b00048792ef7/e/313a6c018bed8f931e1776a2

    annotation { "Feature Type Name" : "Edge Prep" }
    export const edgePrep = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            // Define the parameters of the feature type
            
            // This is where your user interface goes
            
            annotation { "Name" : "Face", "Filter" : EntityType.FACE, "MaxNumberOfPicks" : 1 }
            definition.face is Query;
            
        }
        {
            // Define the function's action
            
            // Create a plane at the top right corner of the selected face
            const facePlane = evFaceTangentPlane(context, {
                    "face" : definition.face,
                    "parameter" : vector(1, 1) //This means that the plane will be at the top right corner 0,0 is the bottom left
            });
            
            // Create a plane on the right side 
            const sidePlane = plane(facePlane.origin, facePlane.x, -facePlane.normal);
            
            // Debug shows you items in the graphics area or in the monitor
            debug(context, facePlane, DebugColor.BLUE);
            debug(context, sidePlane, DebugColor.RED);
            
            // Sketch on the "frontPlane"
            var faceSketch = newSketchOnPlane(context, id + "sketchFacePlane", {
                    "sketchPlane" : facePlane
            });
            
            skCircle(faceSketch, "circle1", {
                    "center" : vector(0, 0) * inch,
                    "radius" : 1 * inch
            });
            
            skSolve(faceSketch);
            
            // Find the sketch region from the sketch
            const faceSketchToExtrude = qSketchRegion(id + "sketchFacePlane");
            
            // Extrude the sketch
            opExtrude(context, id + "extrudeFaceSketch", {
                    "entities" : faceSketchToExtrude,
                    "direction" : -facePlane.normal,
                    "endBound" : BoundingType.BLIND,
                    "endDepth" : 0.25 * inch
            });
            
            // Find the extruded part
            const extrudedParts = qCreatedBy(id + "extrudeFaceSketch", EntityType.BODY);
            
            // Subtract parts
            opBoolean(context, id + "booleanRemoveFrontParts", {
                    "tools" : extrudedParts,
                    "targets" : qOwnerBody(definition.face),
                    "operationType" : BooleanOperationType.SUBTRACTION
            });
        });


Answers

  • imants_smidchensimants_smidchens Member Posts: 35 EDU
    there's an "extrude individual" FS that might be useful:
    https://cad.onshape.com/documents/95c00401c440b44ad8799ef5/v/3048d5049faa1f7427c2ed5c/e/f59ee8c28530122eb7fa9f5c
    which lets you specify different extrude details for different selections. If you're set on making your own tool, however, I think most of the concepts from the 90sec FS tutorial on https://cad.onshape.com/FsDoc/ should be applicable. For getting the UI to look and operate like the official Extrude feature, you can heavily reference how other custom features achieve things you want, and port over applicable code.
  • KyleDarnellKyleDarnell Member Posts: 7 PRO
    edited February 23
    there's an "extrude individual" FS that might be useful:
    https://cad.onshape.com/documents/95c00401c440b44ad8799ef5/v/3048d5049faa1f7427c2ed5c/e/f59ee8c28530122eb7fa9f5c
    which lets you specify different extrude details for different selections. If you're set on making your own tool, however, I think most of the concepts from the 90sec FS tutorial on https://cad.onshape.com/FsDoc/ should be applicable. For getting the UI to look and operate like the official Extrude feature, you can heavily reference how other custom features achieve things you want, and port over applicable code.
    I see what you are talking about but, I'm looking for more of an automated FS. Basically, I want to just select a surface and the FS automatically make the sketch and cut into it. Is that something feasible. I want the least bit of user input possible.
  • _anton_anton Member, Onshape Employees Posts: 74
    Do you need a feature that makes multiple extrudes in different directions? They'll all need their own end conditions, at which point it might be easier to just use multiple extrude features.

    Else, can you share a simple example of what this feature would do that you'd normally do with multiple features?

    If you do want to write a custom feature, a great way to start is by copying our Extrude feature from here into a Feature Studio and modifying it as needed.
  • KyleDarnellKyleDarnell Member Posts: 7 PRO
    _anton said:
    Do you need a feature that makes multiple extrudes in different directions? They'll all need their own end conditions, at which point it might be easier to just use multiple extrude features.

    Else, can you share a simple example of what this feature would do that you'd normally do with multiple features?

    If you do want to write a custom feature, a great way to start is by copying our Extrude feature from here into a Feature Studio and modifying it as needed.
    There is a link on the initial post to the document that shows what extrudes I will be using. It will be in a folder named "FEATURE SCRIPT?" I will also look into your FS that you have linked.
  • MichaelPascoeMichaelPascoe Member Posts: 835 PRO
    edited February 24 Answer ✓
    @KyleDarnell Welcome to FeatureScript!
    Here are the general steps you will need to be able to do this:

    https://cad.onshape.com/documents/82e1c77948a53493dda93e14/w/903175619ca8b00048792ef7/e/313a6c018bed8f931e1776a2

    annotation { "Feature Type Name" : "Edge Prep" }
    export const edgePrep = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            // Define the parameters of the feature type
            
            // This is where your user interface goes
            
            annotation { "Name" : "Face", "Filter" : EntityType.FACE, "MaxNumberOfPicks" : 1 }
            definition.face is Query;
            
        }
        {
            // Define the function's action
            
            // Create a plane at the top right corner of the selected face
            const facePlane = evFaceTangentPlane(context, {
                    "face" : definition.face,
                    "parameter" : vector(1, 1) //This means that the plane will be at the top right corner 0,0 is the bottom left
            });
            
            // Create a plane on the right side 
            const sidePlane = plane(facePlane.origin, facePlane.x, -facePlane.normal);
            
            // Debug shows you items in the graphics area or in the monitor
            debug(context, facePlane, DebugColor.BLUE);
            debug(context, sidePlane, DebugColor.RED);
            
            // Sketch on the "frontPlane"
            var faceSketch = newSketchOnPlane(context, id + "sketchFacePlane", {
                    "sketchPlane" : facePlane
            });
            
            skCircle(faceSketch, "circle1", {
                    "center" : vector(0, 0) * inch,
                    "radius" : 1 * inch
            });
            
            skSolve(faceSketch);
            
            // Find the sketch region from the sketch
            const faceSketchToExtrude = qSketchRegion(id + "sketchFacePlane");
            
            // Extrude the sketch
            opExtrude(context, id + "extrudeFaceSketch", {
                    "entities" : faceSketchToExtrude,
                    "direction" : -facePlane.normal,
                    "endBound" : BoundingType.BLIND,
                    "endDepth" : 0.25 * inch
            });
            
            // Find the extruded part
            const extrudedParts = qCreatedBy(id + "extrudeFaceSketch", EntityType.BODY);
            
            // Subtract parts
            opBoolean(context, id + "booleanRemoveFrontParts", {
                    "tools" : extrudedParts,
                    "targets" : qOwnerBody(definition.face),
                    "operationType" : BooleanOperationType.SUBTRACTION
            });
        });


  • KyleDarnellKyleDarnell Member Posts: 7 PRO
    @KyleDarnell Welcome to FeatureScript!
    Here are the general steps you will need to be able to do this:

    https://cad.onshape.com/documents/82e1c77948a53493dda93e14/w/903175619ca8b00048792ef7/e/313a6c018bed8f931e1776a2

    annotation { "Feature Type Name" : "Edge Prep" }
    export const edgePrep = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            // Define the parameters of the feature type
            
            // This is where your user interface goes
            
            annotation { "Name" : "Face", "Filter" : EntityType.FACE, "MaxNumberOfPicks" : 1 }
            definition.face is Query;
            
        }
        {
            // Define the function's action
            
            // Create a plane at the top right corner of the selected face
            const facePlane = evFaceTangentPlane(context, {
                    "face" : definition.face,
                    "parameter" : vector(1, 1) //This means that the plane will be at the top right corner 0,0 is the bottom left
            });
            
            // Create a plane on the right side 
            const sidePlane = plane(facePlane.origin, facePlane.x, -facePlane.normal);
            
            // Debug shows you items in the graphics area or in the monitor
            debug(context, facePlane, DebugColor.BLUE);
            debug(context, sidePlane, DebugColor.RED);
            
            // Sketch on the "frontPlane"
            var faceSketch = newSketchOnPlane(context, id + "sketchFacePlane", {
                    "sketchPlane" : facePlane
            });
            
            skCircle(faceSketch, "circle1", {
                    "center" : vector(0, 0) * inch,
                    "radius" : 1 * inch
            });
            
            skSolve(faceSketch);
            
            // Find the sketch region from the sketch
            const faceSketchToExtrude = qSketchRegion(id + "sketchFacePlane");
            
            // Extrude the sketch
            opExtrude(context, id + "extrudeFaceSketch", {
                    "entities" : faceSketchToExtrude,
                    "direction" : -facePlane.normal,
                    "endBound" : BoundingType.BLIND,
                    "endDepth" : 0.25 * inch
            });
            
            // Find the extruded part
            const extrudedParts = qCreatedBy(id + "extrudeFaceSketch", EntityType.BODY);
            
            // Subtract parts
            opBoolean(context, id + "booleanRemoveFrontParts", {
                    "tools" : extrudedParts,
                    "targets" : qOwnerBody(definition.face),
                    "operationType" : BooleanOperationType.SUBTRACTION
            });
        });


    This is exactly what I was looking for to get me started. Thank you.
  • GregBrownGregBrown Member, Onshape Employees Posts: 28
    The challenge with these type of hyper-specific custom features is to make them "general enough" to be resilient in a variety of expected use cases, yet not need 1000+ lines of complicated error checking and logic. You can check the standard feature library for things like extrude.fs and  see just how much code goes into making these general features robust in all possible cases! 

    Anyway, the other challenge is to make the user input only the bare minimum and use the programming to infer the rest of it. I experimented with this UI: 



    and still have it work well enough (NOTE: I only hacked this up quickly and so far it only treats one end of the slot... still needs more work!) It could be done many ways, but I like mate connectors!






Sign In or Register to comment.