Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape, CAD, maker project and design.

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.

Conditional range selections

murat_bakkalmurat_bakkal Member Posts: 7 PRO
edited May 2018 in FeatureScript
Hello everyone,

I am trying to build a screw generator. I want to extract information from three tables that have different classes of diameters and for each diameter, there is a range of screw lengths that the user can choose from. This is one of the tables:



Depending on the selection of the screw type and screw diameter (which is dependent on the screw type), I would like to show to the user, only the relevant range of screw lengths. What I want to achieve is to get a set of variables from the user, like screw diameter, screw length, screw head size and so on.

My initial idea for the precondition is like this:

annotation { "Name" : "Screw type", "UIHint" : "SHOW_LABEL" }
        definition.screwType is ScrewType;                    // exported ScrewType enum that has 3 selections

        if (definition.screwType == ScrewType.fpan)
        {
            annotation { "Name" : "Screw dia", "UIHint" : "SHOW_LABEL" }
            definition.screwDiamFPH is ScrewDiaFPH;                                   // diameter classes for this type of screw

            if (definition.screwDiamFPH == ScrewDiaFPH.twotwo)                // if 2.2 dia screw is chosen
            {
                annotation { "Name" : "Screw Length", "UIHint" : "SHOW_LABEL" } // show the enum for this spec. range
                definition.fPTwoTwoL is fPTwoTwoLengths;
            }
            if (definition.screwDiamFPH == ScrewDiaFPH.twofive)
            {
                annotation { "Name" : "Screw Length", "UIHint" : "SHOW_LABEL" }
                definition.fPTwoFiveL is fPTwoFiveLengths;
            }
            if (definition.screwDiamFPH == ScrewDiaFPH.threezero)
            {
                annotation { "Name" : "Screw Length", "UIHint" : "SHOW_LABEL" }
                definition.fPThreeL is fPThreeLengths;
            }

and so on. The same would be done for the other two classes of screws. Inside the program function, I would construct the same structure of if statements and assign the values of the selections to one common variable (for example screwDia or screwLength).

I found myself exporting and defining an enum for each and every specific case. I am new to Feature Script and a novice programmer but this did not feel right to me. First of all, I am showing a different enum to the user for the same information (screwLength variable) and under the same label which feels like "cheating". Furthermore, every time I change a more general condition (like screw type selection) the dependent selection range resets its value because it is actually a different annotation. I would very much prefer to keep this value as long as possible to give flexibility to the user. 

Is there a way to make an enum change its form conditionally? I tried to move my if statements inside the "export enum ScrewDia" bit so that the enum structure would change its' values and tags by itself but that did not work. Is there an appropriate syntax for this?

I have also read from the forums that a look-up table would be a more suitable way to approach this but I could not adapt example codes to this specific case. Could someone please show me how the general structure for a look-up table for this particular case should look like?

Cheers.

Murat BAKKAL 


Best Answer

Answers

  • murat_bakkalmurat_bakkal Member Posts: 7 PRO
    edited May 2018
    Thank you for the pointer. I am aware of one other custom feature that does this (PP_Capscrew) but I couldn't see the source code of it. I am trying to do it on my own to learn some Feature Script and also incorporate it later to another custom feature project that makes screw ports on plastics, which will be tailored to the design-process habits of the company that I work at. The screws are Plastofast type screws from a Würth catalog.
  • philip_thomasphilip_thomas Member, Moderator, Onshape Employees, Developers Posts: 1,186
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 1,885
    The Beams feature also uses lookup tables extensively. 
    Neil Cooke, Director of Technical Marketing, Onshape Inc.
  • murat_bakkalmurat_bakkal Member Posts: 7 PRO
    @philip_thomas , @NeilCooke thank you for the information. I am studying the lookup tables.

Sign In or Register to comment.