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.

importing a FS into a document

billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
edited July 2016 in FeatureScript
So I'm using the same featurescript code over and over in different documents by copying the source code between documents. If I make any changes to the featurescript, I have to remember which document has the latest featurescript code to copy from.

My thought was to create a document called "featurescripts" and then import featurescript code from this versioned document. You know, take advantage of versioning.

I have 2 documents:
-featurescripts
-FS print

Here's "FS print" working and the featurescript code:


Here's "FS print" working and the part studio:



"FS print" is working, so let's break it and send the working part of the code to "featurescript".

Take the working code out of 
document "FS print":

paste into document "featurescripts" and then create a version of the document:


go back to document "FS print" and import featurescript:


it doesn't work


I've tried moving the data back to document "FS print" without success:







I'm interested in importing my featurescript and taking advantage of version control. Can't seem to get things to wire up properly. Is this possible?

When importing, how do I access a previous version of a document "featurescript"?

If I ever get this to work and create a new version of the document "featurescript", will everything blow apart?

Since only documents can be versioned, is it wise to have a document for each featurescript and not gang all featurescripts together into one document?






Comments

  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    FeatureScript requires imports to all be at the top – before any other top level constructs. The error "extraneous import 'import'" just means our parser doesn't recognize imports after it thinks it's done parsing all the imports.

    If you move the line "const data = BLOB_DATA.csvData" below the import, that error should go away.

  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    That doesn't seem to work.

    To simplify things, I'm adding both listing again.

    This comes out of my "featurescripts" document and includes the code I want to import:


    This is the working document "FS Print" and I want to include code from the document "featurescripts" which is at version v5.

    Any other ideas? 

    This seems like it would be a very common pattern that many will want to use. What am I doing wrong?
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,211
    You may need to put the "export" keyword before the import for the printme feature to become available.  As for data, the feature studio you're importing ("test print") cannot access it from within the feature studio that's doing the importing ("print").  I suggest making a wrapper feature in "print" that passes data to the shared function as a parameter.  In the future, we'll make it possible to pick a blob element as a feature parameter directly from the part studio -- that should address this case better.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    edited July 2016
    Ok so I think I have this working. Thanks Ilya!

    Once again this is what I want:

    I want to re-use the same feature script in multiple documents. If you do this, the document "featurescript" must be versioned.

    It's important to note that I'm using different imported data for each document ("FS print", "billy's print" and "ugly print"). This means that I have split the feature script into 2 location and pass the data from one document to another using setVariable.

    So here's the feature script code I want to share, document name is "featurescripts":
    <div>//written by Bill Campbell 2016- billy@rustyshed.com
    FeatureScript 370;
    import(path : "onshape/std/geometry.fs", version : "370.0");
    
    </div><div>annotation { "Feature Type Name" : "printMe" }
    
    export
    
    const printMe = defineFeature(function(context is Context, id is Id, definition is map)</div><div>    precondition
             {
                     // Define the parameters of the feature type
             }
             {
                      // pass data in with global variable
                      println(getVariable(context, 'billysData'));
             });</div><div></div>

    Here's the code in any document, document name is "FS print":
    <div>//written by Bill Campbell 2016- billy@rustyshed.com
    FeatureScript 370;
    import(path : "onshape/std/geometry.fs", version : "370.0");
    
    //import print.csv
    import(path : "d46479ed26394c59bc644b2b", version : "0e6142d85c6d9c19f3a404b9");
    
    //because Ilya says
    export
    
    </div><div>//import script from document "featurescript"
    
    import(path : "ce7c8d94b49a7f5b94ec5872/8584010f426f2ef23c03aed7/1a8019db9bab99c1bd4517e9", version : "689d1671e01522d3319f531d");
    annotation { "Feature Type Name" : "getData" }
    export const getData = defineFeature(function(context is Context, id is Id, definition is map)</div><div>
    precondition
      &nbsp; {
        &nbsp; &nbsp; // Define the parameters of the feature type
    &nbsp; &nbsp; }
    &nbsp; &nbsp; {
        &nbsp; &nbsp; setVariable(context, 'billysData', BLOB_DATA.csvData);
        &nbsp; &nbsp; &nbsp; &nbsp; //println(BLOB_DATA.csvData);
    &nbsp; &nbsp; });</div>
    Notes:
    - shared feature script "featurescript" must be in a versioned document
    - my any document "FS print" has data it's sending to shared script
    - I had to process data in each any document then send it shared script
    - one shared featurescript under version control, fantastic

    If anyone has a cleaner pattern, please post,



Sign In or Register to comment.