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.

Featurescript to automate creation of entities with varying settings

paul_mumbypaul_mumby Member Posts: 2
edited October 8 in Community Support

Hello, I've been fighting with how to do this for some time, I know in the time I've spent I could have just hand modelled the solution, but I keep thinking there MUST be a way to do this…

I have a reasonably complex profile, in a sketch (artistic, hand drawn). Aspects of the profile (some lengths, and offsets via constraints) are driven by an external "index" value, so index 1 results in one shape, index 2 another shape, and so forth. The total number of objects is also a variable (in a variable studio). Currently it's 36, but it could be anywhere from 20 to 100 or more…

I have a script together that creates a customized circular pattern, which uses the sketch, and a center axis, to extrude the profile to a thickness, then move around, extruding separate parts, while doing a setVariable() inside the for loop… Unfortunately while this works in that it creates 38 copies of the profile extruded in a circle, it does not work in that they are alll identical, they don't honor/reference the updated "index" value.

Is there a way to do this that I'm missing?

Here is the feature script so far:

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

annotation { "Feature Type Name" : "Indexed Parametric Circular Pattern", "Creates a circular pattern of extrusions from a sketch, while adjusting an index variable within the sketch" : "" }
export const parametricCircularPattern = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        // Define the parameters of the feature type
        annotation { "Name" : "Sketch to Pattern", "Filter" : EntityType.FACE && SketchObject.YES, "MaxNumberOfPicks" : 1 }
        definition.faceToPattern is Query;

        annotation { "Name" : "Axis of Pattern", "Filter" : EntityType.EDGE, "MaxNumberOfPicks" : 1 }
        definition.axisOfPattern is Query;

        annotation { "Name" : "Entity Count" }
        isInteger(definition.instanceCount, POSITIVE_COUNT_BOUNDS);

        annotation { "Name" : "Total Angle for Pattern" }
        isAngle(definition.totalAngle, ANGLE_360_BOUNDS);

        annotation { "Name" : "Face Extrusion Depth per Instance" }
        isLength(definition.depth, LENGTH_BOUNDS);        

    }
    //Action Block
    
    const axisLine = evLine(context, { "edge" : definition.axisOfPattern });

    // Calculate the angle step between each instance.
    const angleStep = definition.totalAngle / definition.instanceCount;

    // The core 'for' loop.
    for (var i = 0; i < definition.instanceCount; i += 1)
    {
        // Create a unique ID for each operation inside the loop.
        const localId = id + i;

        // 2. THIS IS THE KEY STEP: Set the variable for the context of the next operation.
        // When opExtrude evaluates the sketch, it will use this new value.
        setVariable(context, "index", i+1);
        
        // Somehow need to apply this to the sketch.... ????
        
        // 3. Generate the geometry using the user-provided sketch profile.
        opExtrude(context, localId + "extrude", { 
                    "entities" : definition.faceToPattern, // Reference the selected sketch face
                    "direction" : evOwnerSketchPlane(context, {"entity" : definition.faceToPattern}).normal,
                    "startBound" : BoundingType.BLIND,
                    "startDepth" : definition.depth/2,
                    "endBound" : BoundingType.BLIND,
                    "endDepth" : definition.depth/2,
                });

        // 4. Transform the newly created body into its correct rotational position.
        const currentAngle = i * angleStep;

        const bodyToTransform = qCreatedBy(localId + "extrude", EntityType.BODY);

        opTransform(context, localId + "transform", {
            "bodies" : bodyToTransform,
            "transform" : rotationAround(axisLine, currentAngle)
        });
    }

    setVariable(context, "index", 1);

});
Tagged:

Answers

  • Caden_ArmstrongCaden_Armstrong Member Posts: 334 PRO

    If I understand correctly, the sketch feature exists in the same feature tree as your custom feature, and it reads the index variable to determine the shape.

    The issue is that features later in a feature tree can't update past features. Once the sketch has been resolved, setting the variable index later on does not effect the already resolved sketch. Think of the feature tree like time travel - future events do not effect past events.

    I think to achieve what you are looking for, your source shape sketch will need to be in a configured part studio (with index as a configuration parameter), and then you will have to instantiate (https://cad.onshape.com/FsDoc/library.html#module-instantiator.fs) the configured sketch into your destination part studio.

    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications
  • EvanReeseEvanReese Member, Mentor Posts: 2,617 PRO

    Caden's solution will probably do it because he knows his stuff, but I also think you may be looking for the feature pattern behavior, like in the Circular Pattern feature with "Apply per instance" checked, where the sketch feature actually regenerates each time. That's done with applyPattern(). You'd need to change your "Sketch to pattern" parameter to a FeatureList parameter too. You can check the Circular Pattern feature code for an example.

    Without knowing your application I'm actually not sure which of these approaches I'd recommend.

    Evan Reese
    The Onsherpa | Reach peak Onshape productivity
    www.theonsherpa.com
Sign In or Register to comment.