How to make a duplicate of a part in featurescript

leti_ryder
leti_ryder Member Posts: 7 EDU
Hi guys!
I am trying to make a duplicate of a part in featurescript, I am using opPattern which requires a transform input. I am not sure what to enter.


   var bfsBody = qCreatedBy(sketchId + "revolve1", EntityType.BODY);
        var bfsBody = qCreatedBy(sketchId + "revolve1");
        debug(context, bfsBody);
        
        opPattern(context, id + "pattern1", {
                "entities" : bfsBody,
                "transforms" :  transform(vector(1, 2, 50) * millimeter),
                "instanceNames" :  "scaled_body"
        });
    

Answers

  • mahir
    mahir Member, Developers Posts: 1,324 ✭✭✭✭✭
    I believe the input for "transforms" is an array of transforms. So you'll need to add brackets [] around the transform command to turn it into an array of one transform. The transform dictates where exactly the copy is going to be placed. In your example, you're using a transform with a single distance vector input of (1, 2, 50) * millimeter. That means your copy will be moved 1/2/50mm in x/y/z. Check out the documentation at the link below for details on more complicated transforms involving scaling/rotation along with specific syntax.

    https://cad.onshape.com/FsDoc/library.html#module-transform.fs
  • MichaelPascoe
    MichaelPascoe Member Posts: 2,958 PRO
    edited December 2022

    Echoing @mahir. opPattern needs a "transform" of some sort to know what to do with the new item.
    The transform and the name must be in an array [ ]. You can transform the new patterned entity to its original position like this:
        var bfsBody = qCreatedBy(sketchId + "revolve1", EntityType.BODY);
        var bfsBody = qCreatedBy(sketchId + "revolve1");
        debug(context, bfsBody);
    
        opPattern(context, id + "pattern1", {
                    "entities" : bfsBody,
                    "transforms" : [transform(XY_PLANE, XY_PLANE)],
                    "instanceNames" : ["scaled_body"]
                });

    If you wanted to pattern multiple items the transforms and the names must be separated by a comma:
        var bfsBody = qCreatedBy(sketchId + "revolve1", EntityType.BODY);
        var bfsBody = qCreatedBy(sketchId + "revolve1");
        debug(context, bfsBody);
    
        opPattern(context, id + "pattern1", {
                    "entities" : bfsBody,
                    "transforms" : [transform(XY_PLANE, XY_PLANE), transform(XY_PLANE, XY_PLANE), transform(XY_PLANE, XY_PLANE)],
                    "instanceNames" : ["scaled_body1", "scaled_body2", "scaled_body3"]
                });


    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚
  • EvanReese
    EvanReese Member, Mentor Posts: 3,091 PRO
    Michael is right on the money here. If you don't want to move it in any way you can use identityTransform() to make a transform that doesn't do anything instead of transform(XY_PLANE, XY_PLANE)
    Evan Reese
    The Onsherpa | Reach peak Onshape productivity
    www.theonsherpa.com