Generate Featurescript from viewer

arpit_agarwal087
arpit_agarwal087 Member Posts: 9 EDU
Is it possible to generate feature script after creating model using onshape viewer? 

Comments

  • EvanReese
    EvanReese Member, Mentor Posts: 3,099 PRO
    do you mean using the "Show code" selection? If so, then not exactly. Maybe you're interested in creating a feature that instantiates configurations of a certain Part Studio? Or maybe I'm way off. Will you clarify?
    Evan Reese
    The Onsherpa | Reach peak Onshape productivity
    www.theonsherpa.com
  • arpit_agarwal087
    arpit_agarwal087 Member Posts: 9 EDU
    I am not sure I know what is "Show code" selection. I have a document which I created using onshape web viewer. I want to obtain the code/instruction which could generate the same document. I want to subsequently reference this code to create a python code or a Feature script to generate the similar documents. 
    Let me know this explanation is better. 
  • billy2
    billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,206 PRO
    edited March 2023
    There's no way to grab featurescript code to create a viewer.

    You need to have an OS account, a server, and api keys. Then you make a call to OS (API) and extract the gltf data. Most people use three.js which now has a gtlf importer and you spin a OS model on your web-site.

    At this point, you've never used featurescript.


  • S1mon
    S1mon Member Posts: 4,637 PRO
    If you right click on the part studio tab, there's a menu item called "show code". This shows the commands used to create all the parts in a part studio. The featurescript code is automatically generated, and with sketches, it's especially hard to follow.

    Here's a really simple part studio with one sketch and one extrude.



    Here's the code:

    FeatureScript 1993;
    import(path : "onshape/std/geometry.fs", version : "1993.0");
    
    annotation { "Default Units" : ["cubicMillimeter", "degree", "degreePerSecond", "footPoundForce", "inchPerSecondSquared", "inchPound", "kilogram", "millimeter", "poundForce", "poundPerSquareInch", "second", "squareMillimeter"] }
    export function main()
    {
        return build({});
    }
    
    export function build()
    {
        return buildPrivate({});
    }
    
    export function build(configuration is map)
    {
        return buildPrivate(configuration);
    }
    
    const buildPrivate = definePartStudio(function(context is Context, configuration is map, lookup is function)
        precondition
        {
        }
        {
            const id is Id = newId();
            annotation { 'unused' : true }
            var features = {};
            features.FttZ42IO6TvvBJH_0 = function(id)
                {
                    annotation { 'unused' : true }
                    var features = features;
                    if (true)
                    {
                        const initialGuessFttZ42IO6TvvBJH_0 = { "U03A415rXXzm.bottom" : [-0.010639444342187937, -0.012500000000000004, -1.0, -1.2246467991473532E-16, -0.03563944434218794, 0.014360555657812064], "U03A415rXXzm.top" : [4.77156689227499E-19, 0.0125, -1.0, -1.2246467991473532E-16, -0.025, 0.025], "U03A415rXXzm.left" : [0.025, 0.003896280050376263, 6.123233995736765E-17, 1.0, -0.01639628005037626, 0.008603719949623742], "U03A415rXXzm.right" : [-0.025, 0.0038962800503762663, 6.123233995736765E-17, 1.0, -0.016396280050376272, 0.008603719949623731], "U03A415rXXzm.middle" : [0.0, 0.0], "qwTZyLEcdndP" : [0.060000000000000005, 4.3368086899420177E-19, 1.0, 0.0, 0.025, -1.0], "utcMq0lFPrP3" : [3.141592653589793] };
                        {
                        }
                        var APuYXJMdfFFigm_query;
                        APuYXJMdfFFigm_query=qCompressed(1.0,"%B5$QueryM4Sa$entityTypeBa$EntityTypeS4$FACESb$historyTypeS8$CREATIONSb$operationIdB2$IdA1S3.7$TopplaneOpS9$queryTypeS5$DUMMY",id);
                        annotation { "Feature Name" : "Sketch 1" }
                        var sketch = newSketch(context, id + "FttZ42IO6TvvBJH_0", { "sketchPlane" : qUnion([APuYXJMdfFFigm_query]), "disableImprinting" : false });
                        skLineSegment(sketch, "U03A415rXXzm.bottom", {});
                        skLineSegment(sketch, "U03A415rXXzm.top", {});
                        skLineSegment(sketch, "U03A415rXXzm.left", {});
                        skLineSegment(sketch, "U03A415rXXzm.right", {});
                        skPoint(sketch, "U03A415rXXzm.middle", { "construction" : true });
                        skCircle(sketch, "qwTZyLEcdndP", {});
                        {
                            skConstraint(sketch, "U03A415rXXzm.mid1", { "constraintType" : ConstraintType.MIDPOINT, "localMidpoint" : "U03A415rXXzm.middle", "localEntity1" : "U03A415rXXzm.top.start", "localEntity2" : "U03A415rXXzm.bottom.end" });
                        }
                        {
                            skConstraint(sketch, "U03A415rXXzm.mid2", { "constraintType" : ConstraintType.MIDPOINT, "localMidpoint" : "U03A415rXXzm.middle", "localEntity1" : "U03A415rXXzm.top.end", "localEntity2" : "U03A415rXXzm.bottom.start" });
                        }
                        {
                            skConstraint(sketch, "U03A415rXXzm.perpendicular", { "constraintType" : ConstraintType.PERPENDICULAR, "localFirst" : "U03A415rXXzm.top", "localSecond" : "U03A415rXXzm.left" });
                        }
                        {
                            skConstraint(sketch, "U03A415rXXzm.parallel.1", { "constraintType" : ConstraintType.PARALLEL, "localFirst" : "U03A415rXXzm.bottom", "localSecond" : "U03A415rXXzm.top" });
                        }
                        {
                            skConstraint(sketch, "U03A415rXXzm.parallel.2", { "constraintType" : ConstraintType.PARALLEL, "localFirst" : "U03A415rXXzm.left", "localSecond" : "U03A415rXXzm.right" });
                        }
                        {
                            skConstraint(sketch, "U03A415rXXzm.horizontal", { "constraintType" : ConstraintType.HORIZONTAL, "localFirst" : "U03A415rXXzm.top" });
                        }
                        {
                            skConstraint(sketch, "U03A415rXXzm.corner0", { "constraintType" : ConstraintType.COINCIDENT, "localFirst" : "U03A415rXXzm.bottom.start", "localSecond" : "U03A415rXXzm.left.start" });
                        }
                        {
                            skConstraint(sketch, "U03A415rXXzm.corner1", { "constraintType" : ConstraintType.COINCIDENT, "localFirst" : "U03A415rXXzm.bottom.end", "localSecond" : "U03A415rXXzm.right.start" });
                        }
                        {
                            skConstraint(sketch, "U03A415rXXzm.corner2", { "constraintType" : ConstraintType.COINCIDENT, "localFirst" : "U03A415rXXzm.top.start", "localSecond" : "U03A415rXXzm.left.end" });
                        }
                        {
                            skConstraint(sketch, "U03A415rXXzm.corner3", { "constraintType" : ConstraintType.COINCIDENT, "localFirst" : "U03A415rXXzm.top.end", "localSecond" : "U03A415rXXzm.right.end" });
                        }
                        {
                            var IHbzwiSejlhNlX_query;
                            IHbzwiSejlhNlX_query=qCompressed(1.0,"%B5$QueryM4Sa$entityTypeBa$EntityTypeS6$VERTEXSb$historyTypeS8$CREATIONSb$operationIdB2$IdA1S6.7$OriginpointOpS9$queryTypeS5$DUMMY",id);
                            skConstraint(sketch, "U03A415rXXzm.middle.positionSnap0", { "constraintType" : ConstraintType.COINCIDENT, "localFirst" : "U03A415rXXzm.middle", "externalSecond" : qUnion([IHbzwiSejlhNlX_query]) });
                        }
                        {
                            skConstraint(sketch, "Oe43HLykYPma", { "constraintType" : ConstraintType.DISTANCE, "localFirst" : "U03A415rXXzm.left", "localSecond" : "U03A415rXXzm.right", "direction" : DimensionDirection.MINIMUM, "length" : { 'value' : try(50 * millimeter), 'expression' : "50 mm" }.value, "halfSpace0" : DimensionHalfSpace.LEFT, "halfSpace1" : DimensionHalfSpace.RIGHT, "labelRatio" : 0.5, "labelDistance" : 0.02029716866440356 * meter });
                        }
                        {
                            skConstraint(sketch, "QMau08rnmc39", { "constraintType" : ConstraintType.DISTANCE, "localFirst" : "U03A415rXXzm.top", "localSecond" : "U03A415rXXzm.bottom", "direction" : DimensionDirection.MINIMUM, "length" : { 'value' : try(25 * millimeter), 'expression' : "25 mm" }.value, "halfSpace0" : DimensionHalfSpace.LEFT, "halfSpace1" : DimensionHalfSpace.RIGHT, "labelRatio" : 0.5000000000000001, "labelDistance" : -0.03160150380835387 * meter });
                        }
                        {
                            var cwmbTMLobBRBIk_query;
                            cwmbTMLobBRBIk_query=qCompressed(1.0,"%B5$QueryM4Sa$entityTypeBa$EntityTypeS6$VERTEXSb$historyTypeS8$CREATIONSb$operationIdB2$IdA1S6.7$OriginpointOpS9$queryTypeS5$DUMMY",id);
                            skConstraint(sketch, "qwTZyLEcdndP.centerSnap0", { "constraintType" : ConstraintType.HORIZONTAL, "localFirst" : "qwTZyLEcdndP.center", "externalSecond" : qUnion([cwmbTMLobBRBIk_query]) });
                        }
                        {
                            skConstraint(sketch, "pkGE8wXJuJBy", { "constraintType" : ConstraintType.DIAMETER, "localFirst" : "qwTZyLEcdndP", "length" : { 'value' : try(50 * millimeter), 'expression' : "50 mm" }.value, "labelRatio" : 0.3487674442202755, "labelAngle" : -0.8087071199222863 * radian });
                        }
                        {
                            skConstraint(sketch, "utcMq0lFPrP3", { "constraintType" : ConstraintType.DISTANCE, "localFirst" : "U03A415rXXzm.left", "localSecond" : "qwTZyLEcdndP", "direction" : DimensionDirection.MINIMUM, "length" : { 'value' : try(10 * millimeter), 'expression' : "10 mm" }.value, "halfSpace1" : DimensionHalfSpace.RIGHT, "halfSpace0" : DimensionHalfSpace.RIGHT, "labelRatio" : 0.5493069962826285, "labelDistance" : -0.021676426888517227 * meter });
                        }
                        skSetInitialGuess(sketch, initialGuessFttZ42IO6TvvBJH_0);
                        skSolve(sketch);
                    }
                };
            try(features.FttZ42IO6TvvBJH_0(id));
            features.FL1wE06b5naDCJT_0 = function(id)
                {
                    annotation { 'unused' : true }
                    var features = features;
                    if (true)
                    {
                        {
                        }
                        var aTyHdmCjRqaNYh_query;
                        aTyHdmCjRqaNYh_query = qSketchRegion(id + "FttZ42IO6TvvBJH_0", true);
                        annotation { "Feature Name" : "Extrude 1" }
                        extrude(context, id + "FL1wE06b5naDCJT_0", { "domain" : OperationDomain.MODEL, "bodyType" : ToolBodyType.SOLID, "operationType" : NewBodyOperationType.NEW, "surfaceOperationType" : NewSurfaceOperationType.NEW, "flatOperationType" : FlatOperationType.REMOVE, "entities" : qUnion([aTyHdmCjRqaNYh_query]), "surfaceEntities" : qUnion([]), "endBound" : BoundingType.BLIND, "oppositeDirection" : false, "depth" : { 'value' : try(25 * millimeter), 'expression' : "25 mm" }.value, "endBoundEntityFace" : qUnion([]), "endBoundEntityBody" : qUnion([]), "endBoundEntityVertex" : qUnion([]), "hasOffset" : false, "offsetDistance" : { 'value' : try(25 * millimeter), 'expression' : "25 mm" }.value, "offsetOppositeDirection" : false, "hasExtrudeDirection" : false, "extrudeDirection" : qUnion([]), "startOffset" : false, "startOffsetBound" : StartOffsetType.BLIND, "startOffsetDistance" : { 'value' : try(25 * millimeter), 'expression' : "25 mm" }.value, "startOffsetOppositeDirection" : false, "startOffsetEntity" : qUnion([]), "symmetric" : false, "hasDraft" : false, "draftAngle" : { 'value' : try(3 * degree), 'expression' : "3 deg" }.value, "draftPullDirection" : false, "hasSecondDirection" : false, "secondDirectionBound" : BoundingType.BLIND, "secondDirectionOppositeDirection" : true, "secondDirectionDepth" : { 'value' : try(25 * millimeter), 'expression' : "25 mm" }.value, "secondDirectionBoundEntityFace" : qUnion([]), "secondDirectionBoundEntityBody" : qUnion([]), "secondDirectionBoundEntityVertex" : qUnion([]), "hasSecondDirectionOffset" : false, "secondDirectionOffsetDistance" : { 'value' : try(25 * millimeter), 'expression' : "25 mm" }.value, "secondDirectionOffsetOppositeDirection" : false, "hasSecondDirectionDraft" : false, "secondDirectionDraftAngle" : { 'value' : try(3 * degree), 'expression' : "3 deg" }.value, "secondDirectionDraftPullDirection" : false, "defaultScope" : false, "booleanScope" : qUnion([]), "defaultSurfaceScope" : true, "booleanSurfaceScope" : qUnion([]) });
                    }
                };
            try(features.FL1wE06b5naDCJT_0(id));
            return context;
        }, millimeter, {});
    

    The extrude has every single option spelled out, even though if you were hand generating a function to do this, you would only call out the options which are not the defaults.

    Simon Gatrall | Product Development, Engineering, Design, Onshape | Ex- IDEO, PCH, Unagi, Carbon | LinkedIn

  • arpit_agarwal087
    arpit_agarwal087 Member Posts: 9 EDU
    Thanks @S1mon . This is exactly what I was looking for. 
  • khaled_elhady
    khaled_elhady Member Posts: 2 ✭

    Hi @S1mon !
    If I have the automatically generated script, can I use it to re-create the same part studio again?

  • Caden_Armstrong
    Caden_Armstrong Member Posts: 472 PRO

    what are you trying to do?
    In many cases a derive feature is the easiest way to "replicate another part studio".

    www.smartbenchsoftware.com --- Renaissance --- Kestrel -- SmartLink
    Experts in Onshape Automation - Custom Features and Integrated Applications