How do I know that you are FLAT-PATTERN?

Programming and macros
User avatar
mihkov
Posts: 44
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 17
x 24

How do I know that you are FLAT-PATTERN?

Unread post by mihkov »

SM-FLAT-PATTERN
02_Derived_Configuration.jpg
02_Derived_Configuration.jpg (14.65 KiB) Viewed 7739 times
How does the FLAT-PATTERN configuration work? Does it have any identifiers other than the keyword in the name "SM-FLAT-PATTERN"? How can I find out this configuration FLAT-PATTERN, other than a substring in the name?
This substring (SM-FLAT-PATTERN) may be different in different language packs, how can this be tracked?
(English): "SM-FLAT-PATTERN"
(French): "SM-DEPLIE-PLAT"
(German): "SM-FLACH-MUSTER"
(Spanish): "SM-DESPIECE-PLANO"
(Simplified Chinese): "SM-展开图"
If you know, show me how you get all the properties of a given configuration in order to accurately determine who it is? (some like GetConfigurationProperty)
Anyone losing their mind can name the configuration: "SM-FLAT-PATTERN"
by AlexB » Thu Oct 12, 2023 10:41 am
AlexLachance wrote: Thu Oct 12, 2023 9:11 am Isn't there something to call out if flat pattern is true in programming? I thought I saw this once
You're correct. If you use GetBendState, you can check if it's sheet metal as well as if it's flattened or bent.

https://help.solidworks.com/2017/englis ... state.html
Go to full post
User avatar
josh
Posts: 304
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 16
x 22
x 514

Re: How do I know that you are FLAT-PATTERN?

Unread post by josh »

There is nothing special about the flat pattern configuration. I think SW does automatically create it when you create a sheet metal part, but as you’ve found the name depends on the language installed, just like other features get named based on the installed language.

Additionally, there’s nothing preventing the user from unsuppressing the flat pattern feature in the Default configuration, or suppressing the flat pattern feature in the flat pattern configuration. So just because you’ve found a config with “Flat pattern” in the name doesn’t actually mean that the flat pattern is displayed. In fact, you could have multiple different configs of the part itself, each with its own different flat pattern config. I think your best bet will be to find the flat pattern feature in the tree. Iterate through the features and use GetTypeName2 until you find “FlatPattern”. You can then use IsSuppressed2 to figure out which config (or configs) it’s unsuppressed in. Or you can just unsuppress it in the current config.
User avatar
AlexLachance
Posts: 2226
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2419
x 2061

Re: How do I know that you are FLAT-PATTERN?

Unread post by AlexLachance »

josh wrote: Thu Oct 12, 2023 8:55 am There is nothing special about the flat pattern configuration. I think SW does automatically create it when you create a sheet metal part, but as you’ve found the name depends on the language installed, just like other features get named based on the installed language.

Additionally, there’s nothing preventing the user from unsuppressing the flat pattern feature in the Default configuration, or suppressing the flat pattern feature in the flat pattern configuration. So just because you’ve found a config with “Flat pattern” in the name doesn’t actually mean that the flat pattern is displayed. In fact, you could have multiple different configs of the part itself, each with its own different flat pattern config. I think your best bet will be to find the flat pattern feature in the tree. Iterate through the features and use GetTypeName2 until you find “FlatPattern”. You can then use IsSuppressed2 to figure out which config (or configs) it’s unsuppressed in. Or you can just unsuppress it in the current config.
Isn't there something to call out if flat pattern is true in programming? I thought I saw this once
User avatar
AlexB
Posts: 508
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 29
x 274
x 462

Re: How do I know that you are FLAT-PATTERN?

Unread post by AlexB »

AlexLachance wrote: Thu Oct 12, 2023 9:11 am Isn't there something to call out if flat pattern is true in programming? I thought I saw this once
You're correct. If you use GetBendState, you can check if it's sheet metal as well as if it's flattened or bent.

https://help.solidworks.com/2017/englis ... state.html
User avatar
bnemec
Posts: 1954
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2562
x 1411

Re: How do I know that you are FLAT-PATTERN?

Unread post by bnemec »

From what I have seen SW automatically makes a the flat pattern sub-config when a flat view is added to the drawing. We had go-round with this when people were making drawings for parts that had been modeled earlier and where not checked out. So while they had the drawing open all looked well and checked the drawing in. Then later others would open the drawing and the flat view was formed. While the drawing was being worked on, SW silently created the flat view config for the drawing, but is oblivious that the model is in PDM and not checked out (may fire the "File is read only" warning that most PDM users check the never show me again box.) so the model file is not saved or checked in with the flat config. Then when the drawing is opened that view cannot find it's config and drops back to using "Default" or active config.

What is it you are trying to do with the automatically generated "SM-FLAT-PATTERN" config?
User avatar
CarrieIves
Posts: 169
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 388
x 144

Re: How do I know that you are FLAT-PATTERN?

Unread post by CarrieIves »

If you have the option "Automatically populate view pallet with views" checked, it will create the config for you when you start a drawing of the sheetmetal part.
User avatar
mihkov
Posts: 44
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 17
x 24

Re: How do I know that you are FLAT-PATTERN?

Unread post by mihkov »

bnemec wrote: Thu Oct 12, 2023 11:33 am What is it you are trying to do with the automatically generated "SM-FLAT-PATTERN" config?
The gist is this: the user runs the macro directly from the Assembly. Next, all components of the parts are searched at all levels, and if the part is made of sheet metal, then the DXF flat pattern file is saved. The user can choose: Save DXF for all configurations in the found part, or only for those present in the assembly. Accordingly, I need to create a sub-configuration of the FLAT-PATTERN, for configurations that do not have one, and for those that do have one, not to create it. And at the same time, do not create a sub-configuration for the FLAT-PATTERN sub-configuration.

Accordingly, the option that josh suggested is the most correct, I should not care about the name of the configuration a little more than completely, I should check the state of the “FlatPattern” feature.

Tell me, those who have experience. What conditions for a part are: Mandatory and Sufficient. So that I can understand with minimal effort - yes, this is definitely a sheet metal part that has a FlatPattern, thickness, etc. Maybe someone created a folder for her with a table of cuts, created different properties?
To get 100% GetBendState - is this it?
inaf.png
User avatar
mihkov
Posts: 44
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 17
x 24

Re: How do I know that you are FLAT-PATTERN?

Unread post by mihkov »

CarrieIves wrote: Thu Oct 12, 2023 1:53 pm If you have the option "Automatically populate view pallet with views" checked, it will create the config for you when you start a drawing of the sheetmetal part.
I know about this feature for happy users. When we create a drawing for a sheet metal part, a flat pattern sub-configuration is created and only for the currently active configuration.
If before this I manually create a sub-configuration with the name as it should be for the configuration of the “FlatPattern” and do not activate the “FlatPattern” feature, then in the views palette there will be an unexpanded “FlatPattern”, that is, the software simply checks for the presence of the configuration and the name. It's very easy to cheat.
User avatar
bnemec
Posts: 1954
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2562
x 1411

Re: How do I know that you are FLAT-PATTERN?

Unread post by bnemec »

your nested config structure scares me a little bit, but maybe it's for a good purpose.

If you use the "SM-FLAT-PATTERN" config for dxf you won't get bend lines. Save dxfs from the formed config if you want bend lines in dxf.

Here's the dxf method I wrote with tid-bits I picked up from others and SW help doc. Please excuse my literal strings. This is called from the PDM task add-in which provides whether or not to overwrite existing dxf and the method passes back a message that's stored in the task instance details.

In short it:
- to start out there's some logic to build the full dxf path, and if overwrite is set and file exists move it to the "OLD" folder. If the dxf fails I can put it back, if it succeeds then I leave it there.
- gets a list of all the configs, filters that list for any that contian "DXF" then adds "Default" at the end.
+ the "DXF" keyword is an arbitrary phrase that we selected for complicated parts that will not flatten without special edits. Also for laser cut parts that have chamfered holes (our laser cuts out the major diameter).
- next it loops through the list of configs and will use the first one that has a feature TYPE of FlatPattern and that has been working for our needs. Feature type "SheetMetal" might also work. However, I still go clean up the Flat Pattern feature sub-tree because of the random nature of users.
Here's the GetTypeName method I use:
https://help.solidworks.com/2019/Englis ... Redirect=1
- After doing my best to clean up the selected config I set some options that I learned about along the way, make sure the map file is set, etc. then try to save the dxf. That doesn't work try the next config in the list until either we have a dxf or run out of configs.
+ If a good dxf is created by SW, then I add a comment to the top of it at a bread crumb trail to myself. I should add the local version number of the file to that note...

Code: Select all


    /*SaveDXF() needs the part (ModelDoc2 md2) to be active if its a Virtual Component in assembly.
     * returns the path of the saved dxf, if file there and not over written then "" else null.
     */
    private bool? SaveDXF(ModelDoc2 md2, string dxfFileName, bool overWrite, ref string msg)
    {
      string mapFilePath;
      string archivePath = "";
      bool? dxfCreated = null; //stores return value from ExportToDWG2() method.

      string dxfSrcPath = md2.GetPathName();

      PartDoc swPart;
      swPart = (PartDoc)md2;

      string dxfFullPath = System.IO.Path.Combine(getDXFpathByMtrl(ref swPart), dxfFileName);
      dxfFullPath = Path.ChangeExtension(dxfFullPath, "DXF");

      /* truth table for file exists and overwrite
       * exists | overw | action
       * -------------------------
       *    0   |   0   | make new
       *    0   |   1   | make new
       *    1   |   0   | nothing, return msg
       *    1   |   1   | move old then make new
       */
      if (overWrite && File.Exists(dxfFullPath))
      {
        ExtensionsAndFunctions.MoveFileToOLD(dxfFullPath, out archivePath, ref msg);
      }
      if (!File.Exists(dxfFullPath))
      {

        //20201023-bnemec: trying to filter by flat feature in tree. (will be supressed)
        //http://help.solidworks.com/2019/english/api/sldworksapi/Get_All_Sheet_Metal_Feature_Data_Example_CSharp.htm

        //if (!md2.Visible)
        //  md2.Visible = true;

        int actv8DocOptions = (int)swRebuildOnActivation_e.swDontRebuildActiveDoc;
        swApp.ActivateDoc3(dxfSrcPath, false, actv8DocOptions, 0);

        //to help performance per https://forum.solidworks.com/message/858283
        md2.FeatureManager.EnableFeatureTree = false;
        md2.FeatureManager.EnableFeatureTreeWindow = false;

        //look for config with DXF in name else use Default
        bool bShowConfig;
        string[] configs = md2.GetConfigurationNames();
        List<string> dxfConfigs = configs.Where(config => config.ToUpper().Contains("DXF")).ToList();

        //since there may be more than one DXF config we'll use the first one that creates a dxf,
        //so we'll add "Degault" config to the end of the list
        dxfConfigs.Add("Default");
        //20201015-bnemec: testing with configs that contain flat info.
        //20201017 bnemec: commented the if because most legit sheet metal files don't get a flat config
        //that config is only generated in the model if the flat view is added to a drawing.
        /*20201029-bnemec: 
        brought the if back but instead of searching for FLAT I'm looking for the DXF keyword, this is for parts where
        the we need a custom config for dxf creation to work, if a config with DXF in name is found we use that, else use 
        Default config
        This is done: It looks like a better way to do this is look for sheet metal or flat FEATURE in the feature tree
        rather than looking for flat config.
        */

        foreach (var conf in dxfConfigs)
        {
          try
          {
            //activate (show) that config
            bShowConfig = md2.ShowConfiguration2(conf);

            //check to see if the config contains a sheet metal feature with flat pattern
            bool flatPtrnFound = false;
            Feature swFeat = (Feature)md2.FirstFeature();
            while (swFeat != null)
            {
              /* ***TODO***  add logic here to supress non sheet metal stuff if needed, like flex, dome, dimple etc.
               */
              string featName = swFeat.GetTypeName();
              // Process top-level sheet metal features
              if (featName == "FlatPattern")
              {
                flatPtrnFound = true;

                //Make sure the flat feature is supressed, or else the dxf will not have bend lines.
                //http://help.solidworks.com/2019/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.ifeature~setsuppression2.html
                swFeat.SetSuppression2((int)swFeatureSuppressionAction_e.swUnSuppressDependent, (int)swInConfigurationOpts_e.swThisConfiguration, null);

                // unsupress flat pattern sub-features incase someone saved file with them supressed or else the dxf will show them formed.
                Feature swSubFeat;
                swSubFeat = (Feature)swFeat.GetFirstSubFeature();
                while ((swSubFeat != null))
                {
                  string subname = swSubFeat.GetTypeName();
                  string name = swSubFeat.Name;

                  if (swSubFeat.GetTypeName().Contains("UiBend"))
                  {
                    swSubFeat.SetSuppression2((int)swFeatureSuppressionAction_e.swUnSuppressFeature,
                            (int)swInConfigurationOpts_e.swThisConfiguration, null);
                  }
                  swSubFeat = (Feature)swSubFeat.GetNextSubFeature();
                }

                break; // get out of while loop and do not set swFeat to next feature.
              }
              swFeat = (Feature)swFeat.GetNextFeature();
            }
            if (flatPtrnFound)
            {
              object varAlignment;
              double[] dataAlignment = new double[12];
              string[] dataViews = new string[2];
              //instead of setting x,y,z to 1.0, leave them all 0 and it does better job orienting flat.
              dataAlignment[0] = 0.0;
              dataAlignment[1] = 0.0;
              dataAlignment[2] = 0.0;
              dataAlignment[3] = 0.0;
              dataAlignment[4] = 0.0;
              dataAlignment[5] = 0.0;
              dataAlignment[6] = 0.0;
              dataAlignment[7] = 0.0;
              dataAlignment[8] = 0.0;
              dataAlignment[9] = 0.0;
              dataAlignment[10] = 0.0;
              dataAlignment[11] = 0.0;

              varAlignment = dataAlignment;

              /*
            The bitmask for SheetMetalOptions is {12 11 10 9 8 7 6 5 4 3 2 1}, where each bit is either 0 or 1 as follows:
            Bit #1: 1 to export flat-pattern geometry; 0 to not
            Bit #2: 1 to include hidden edges; 0 to not
            Bit #3: 1 to export bend lines; 0 to not
            Bit #4: 1 to include sketches; 0 to not
            Bit #5: 1 to merge coplanar faces; 0 to not
            Bit #6: 1 to export library features; 0 to not
            Bit #7: 1 to export forming tools; 0 to not
            Bit #8: 0
            Bit #9: 0
            Bit #10: 0
            Bit #11: 0
            Bit #12: 1 to export bounding box; 0 to not
               * flat-pattern geometry and bend lines, then Bits 1 and 3 are set, the bitmask is 0000101,
               * and you need to set SheetMetalOptions = 2^0 + 2^2 = 1 + 4 = 5.
               */
              int options = 5;

              // we use a map file for the laser to get bend lines up.  we've always used same settings for laser and thermwood
              //didn't bother implementing map file for each right now.  could be done though.
              swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swDxfMapping, true);
              swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swDXFDontShowMap, true);

              mapFilePath = @"C:\Vault\CAD_Library\SW-DXF_map_LASER";
              swApp.SetUserPreferenceStringListValue((int)swUserPreferenceStringListValue_e.swDxfMappingFiles, mapFilePath);

              int index = swApp.GetUserPreferenceIntegerValue((int)swUserPreferenceIntegerValue_e.swDxfMappingFileIndex);

              swApp.SetUserPreferenceIntegerValue((int)swUserPreferenceIntegerValue_e.swDxfMappingFileIndex, 0);

              index = swApp.GetUserPreferenceIntegerValue((int)swUserPreferenceIntegerValue_e.swDxfMappingFileIndex);

              //Export sheet metal to a single drawing file     
              //https://help.solidworks.com/2019/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IPartDoc~ExportToDWG2.html
              dxfCreated = swPart.ExportToDWG2(dxfFullPath, dxfSrcPath, (int)swExportToDWG_e.swExportToDWG_ExportSheetMetal, true,
              varAlignment, false, false, options, null);
              if (dxfCreated == true && File.Exists(dxfFullPath))
              {
                msg += "Saved \"" + dxfFullPath + LB + "\" from model " + dxfSrcPath + LB +
                  "  Configuration: " + conf + LB;

                // I like adding a note block in the dxf with a bit of info that it was an autommated dxf.
                string note = "999" + NL +
                        "THIS DXF FILE GENERATED FROM SOLIDWORKS BY PDM TASK ADD-IN" +
                        "  FILE: " + dxfSrcPath +
                        "  CONFIGURATION: " + conf +
                        "  AT " + System.DateTime.Now.ToString() +
                        "  USING MAP FILE: " + mapFilePath + NL;
                ExtensionsAndFunctions.prepend(dxfFullPath, note);

                //break out of the while (configs) loop once we have a good dxf.
                break;
              }
              else
              {
                //tried and failed to save dxf from a config that had a FlatPattern feature, this would be an error.
                //dxfCreated should be false here
                msg += "Failed to save dxf from: " + dxfFullPath + "  " + conf;
              }

            }
            else
            {
              //no FlatPattern feature found in this configuration, move on.
              //dxfCreated should = null here.
            }
          }
          catch(Exception ex)
          {
            msg += "caught Exception in foreach(config): " + ex.Message + LB;
          }

        }
        //at end of config foreach loop, if no dxf then need to put the old one back.
        if (!File.Exists(dxfFullPath))
        {
          ExtensionsAndFunctions.MoveFileBack(archivePath, dxfFullPath);
        }
        
      }
      else
      { // file exists and ! overwrite
        msg += (("output file: " + dxfFullPath.Quotes() + " already exists and not set to overwrite.").Highlight() +
          LB);
        dxfFullPath = "";
      }

      return dxfCreated;
    }
    
User avatar
AlexB
Posts: 508
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 29
x 274
x 462

Re: How do I know that you are FLAT-PATTERN?

Unread post by AlexB »

The way I've done it before is the following with my formed or flat configuration open in SW:

- I open a blank drawing template (no border, blocks, etc.)
- I use IDrawingDoc::CreateFlatPatternViewFromModelView3() to add my flat pattern view to my drawing. Note that this adds it for the specified configuration whether it currently exists or not.
- Make sure the user preference to show bend notes is off if you don't want to show the "DOWN 90° R .25" annotations.
- Set the view scale to 1:1
- Call SaveAs with .dxf suffix


I would attach an example but I wrote this specific example for a customer. This should be pretty straight forward if you're moderately familiar with with the API.
User avatar
bnemec
Posts: 1954
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2562
x 1411

Re: How do I know that you are FLAT-PATTERN?

Unread post by bnemec »

AlexB wrote: Fri Oct 13, 2023 2:28 pm The way I've done it before is the following with my formed or flat configuration open in SW:

- I open a blank drawing template (no border, blocks, etc.)
- I use IDrawingDoc::CreateFlatPatternViewFromModelView3() to add my flat pattern view to my drawing. Note that this adds it for the specified configuration whether it currently exists or not.
- Make sure the user preference to show bend notes is off if you don't want to show the "DOWN 90° R .25" annotations.
- Set the view scale to 1:1
- Call SaveAs with .dxf suffix


I would attach an example but I wrote this specific example for a customer. This should be pretty straight forward if you're moderately familiar with with the API.
So that's saving dxf from drawing instead of from model file?
User avatar
AlexB
Posts: 508
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 29
x 274
x 462

Re: How do I know that you are FLAT-PATTERN?

Unread post by AlexB »

bnemec wrote: Fri Oct 13, 2023 4:06 pm So that's saving dxf from drawing instead of from model file?
Technically yes, however the output is the same as exporting it from the part file. There are added steps, however one of the ones it takes care of is determining what flat pattern to output for you and creating that config if it doesn't exist yet.
User avatar
AlexLachance
Posts: 2226
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2419
x 2061

Re: How do I know that you are FLAT-PATTERN?

Unread post by AlexLachance »

bnemec wrote: Fri Oct 13, 2023 4:06 pm So that's saving dxf from drawing instead of from model file?
All our DXF's are created from our drawings. we use CustomTools and have set rules in it that work with sheet names to generate DXF. It also differentiates between letter and 11x17, also does distinction for what needs to be in black and white and what needs to be in colour. Basically we tweak it as we need.
AmenJlili
Posts: 8
Joined: Thu Nov 30, 2023 5:17 pm
Answers: 0
x 6

Re: How do I know that you are FLAT-PATTERN?

Unread post by AmenJlili »

There are many ways to go about this but not one ultimate way.

How about just checking if the derived configuration has a parent configuration and if it has some sheet metal bodies in the feature tree?

Amen
Blue Byte Systems Inc.
cadoverflow.com
User avatar
mihkov
Posts: 44
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 17
x 24

Re: How do I know that you are FLAT-PATTERN?

Unread post by mihkov »

AlexB wrote: Fri Oct 13, 2023 2:28 pm The way I've done it before is the following with my formed or flat configuration open in SW:

- I open a blank drawing template (no border, blocks, etc.)
- I use IDrawingDoc::CreateFlatPatternViewFromModelView3() to add my flat pattern view to my drawing. Note that this adds it for the specified configuration whether it currently exists or not.
- Make sure the user preference to show bend notes is off if you don't want to show the "DOWN 90° R .25" annotations.
- Set the view scale to 1:1
- Call SaveAs with .dxf suffix


I would attach an example but I wrote this specific example for a customer. This should be pretty straight forward if you're moderately familiar with with the API.
Sorry, I didn't understand what this was about. You talk about saving DXF but this is not important at all. I want to find the best algorithm to determine that this part is sheet metal and that it has a flat pattern configuration (or not).
mme
Posts: 9
Joined: Fri Apr 09, 2021 2:11 am
Answers: 0
x 10

Re: How do I know that you are FLAT-PATTERN?

Unread post by mme »

To find out if a part is made of sheet material, you should watch for the presence of "Sheet-metal feaтure" in the tree. "FLAT-PATTERN" will be visible in the Configurations tree only after you have placed a view in the drawing.
Attachments
SW_01.jpg
SW_01.jpg (17.56 KiB) Viewed 7033 times
SW_02.jpg
SW_02.jpg (4.64 KiB) Viewed 7033 times
SW_03.jpg
SW_03.jpg (11.37 KiB) Viewed 7033 times
User avatar
AlexB
Posts: 508
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 29
x 274
x 462

Re: How do I know that you are FLAT-PATTERN?

Unread post by AlexB »

mihkov wrote: Sat Dec 02, 2023 3:18 pm Sorry, I didn't understand what this was about. You talk about saving DXF but this is not important at all. I want to find the best algorithm to determine that this part is sheet metal and that it has a flat pattern configuration (or not).
You mentioned that you were running a macro that looked at all parts in an assembly and exported the DXF flat pattern if it was sheet metal, so it doesn't sound unimportant to you. Others were also discussing methods for exporting the DXF flat pattern so I described my method that is independent of whether a part actually has the derived "SM-FLAT-PATTERN" but is a sheet metal part. So, with this method, all you have to do is determine if it's sheet metal since the export function creates the flat pattern derived configuration if it doesn't exist.

The best method to determine if a part is sheet metal is the following IModelDoc2::GetBendState method. If it returns anything other than 0, it's sheet metal.
https://help.solidworks.com/2017/englis ... state.html
User avatar
bnemec
Posts: 1954
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2562
x 1411

Re: How do I know that you are FLAT-PATTERN?

Unread post by bnemec »

mihkov wrote: Sat Dec 02, 2023 3:18 pm Sorry, I didn't understand what this was about. You talk about saving DXF but this is not important at all. I want to find the best algorithm to determine that this part is sheet metal and that it has a flat pattern configuration (or not).
I guess most of us assumed you are trying to save a dxf of the flat from the model. Is that not what you're trying to do?

If you are trying to automate saving dxfs of flats, that has little (possibly nothing) to do with flat configurations. The automatically generated "[parentFormedCofigName]SM-FLAT-PATTERN" configuration is just for the drawing view. We don't save dxf from flattened configs as the dxf will not have bend lines which we like to have the UP and DOWN bend layers in the dxf.

So what is it you're trying to do? Save dxfs from the part file or find a specific configuration for some other purpose?
User avatar
mihkov
Posts: 44
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 17
x 24

Re: How do I know that you are FLAT-PATTERN?

Unread post by mihkov »

bnemec wrote: Mon Dec 04, 2023 8:53 am I guess most of us assumed you are trying to save a dxf of the flat from the model. Is that not what you're trying to do?

If you are trying to automate saving dxfs of flats, that has little (possibly nothing) to do with flat configurations. The automatically generated "[parentFormedCofigName]SM-FLAT-PATTERN" configuration is just for the drawing view. We don't save dxf from flattened configs as the dxf will not have bend lines which we like to have the UP and DOWN bend layers in the dxf.

So what is it you're trying to do? Save dxfs from the part file or find a specific configuration for some other purpose?
Okay, I'll be frank. Saving DXF is part of my macro, but that's no problem. What I'm really trying to do is check the ASM to make sure the designers have done everything necessary for the project to be considered complete. Namely, I check all the components (Component2) of the assembly. For example:
If the component in all configurations is .IsSuppressed = True - why do we need it at all?
If .Visible = False, then you should pay attention to it.
And so on (.IsVirtual / .IsEnvelope / .IsFixed / ExcludeFromBOM / ToolboxPartType / IsFastener / NoBodyPart / Is Rollback)
Well, in addition to all this, sheet parts must have a dependent SM-FLAT-PATTERN configuration is flat, and also only one for each non-flat configuration.
User avatar
mihkov
Posts: 44
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 17
x 24

Re: How do I know that you are FLAT-PATTERN?

Unread post by mihkov »

AlexB wrote: Mon Dec 04, 2023 8:28 am You mentioned that you were running a macro that looked at all parts in an assembly and exported the DXF flat pattern if it was sheet metal, so it doesn't sound unimportant to you. Others were also discussing methods for exporting the DXF flat pattern so I described my method that is independent of whether a part actually has the derived "SM-FLAT-PATTERN" but is a sheet metal part. So, with this method, all you have to do is determine if it's sheet metal since the export function creates the flat pattern derived configuration if it doesn't exist.

The best method to determine if a part is sheet metal is the following IModelDoc2::GetBendState method. If it returns anything other than 0, it's sheet metal.
https://help.solidworks.com/2017/englis ... state.html
Is the method of getting all the bodies of a component and checking them on "are you a child of a sheet metal feature" worse than GetBendState?
If, before this function, I also check that the part is definitely not in a lightweight (in general, before checking, solve all lightweight components).

Code: Select all

Function IsSheetMetalComponent(comp As SldWorks.Component2) As Boolean
    Dim vBodies As Variant
    vBodies = comp.GetBodies3(swBodyType_e.swSolidBody, Empty)
      If Not IsEmpty(vBodies) Then
        Dim i As Integer
        For i = 0 To UBound(vBodies)
            Dim swBody As SldWorks.Body2
            Set swBody = vBodies(i)
            If False <> swBody.IsSheetMetal() Then
                IsSheetMetalComponent = True
                Exit Function
            End If
        Next
    End If
    IsSheetMetalComponent = False
End Function
User avatar
AlexB
Posts: 508
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 29
x 274
x 462

Re: How do I know that you are FLAT-PATTERN?

Unread post by AlexB »

mihkov wrote: Wed Dec 06, 2023 11:20 am Is the method of getting all the bodies of a component and checking them on "are you a child of a sheet metal feature" worse than GetBendState?
If, before this function, I also check that the part is definitely not in a lightweight (in general, before checking, solve all lightweight components).
It's not worse. It could be more complexity than necessary for a lot of applications but if it works in your program the way it is then I'd leave it in there the way it is.
User avatar
bnemec
Posts: 1954
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2562
x 1411

Re: How do I know that you are FLAT-PATTERN?

Unread post by bnemec »

mihkov wrote: Wed Dec 06, 2023 10:59 am Okay, I'll be frank. Saving DXF is part of my macro, but that's no problem. What I'm really trying to do is check the ASM to make sure the designers have done everything necessary for the project to be considered complete. Namely, I check all the components (Component2) of the assembly. For example:
If the component in all configurations is .IsSuppressed = True - why do we need it at all?
If .Visible = False, then you should pay attention to it.
And so on (.IsVirtual / .IsEnvelope / .IsFixed / ExcludeFromBOM / ToolboxPartType / IsFastener / NoBodyPart / Is Rollback)
Well, in addition to all this, sheet parts must have a dependent SM-FLAT-PATTERN configuration is flat, and also only one for each non-flat configuration.
Ah, You're writing your own design checker. Nice! UU
User avatar
mihkov
Posts: 44
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 17
x 24

Re: How do I know that you are FLAT-PATTERN?

Unread post by mihkov »

AlexB wrote: Wed Dec 06, 2023 12:21 pm It's not worse. It could be more complexity than necessary for a lot of applications but if it works in your program the way it is then I'd leave it in there the way it is.
I did some tests, maybe they are not complete, I could not do other situations except for very trivial ones (when the part is completely empty, etc.). I even created a derived part based on the current one, transferring the sheet metal settings, but got the same exact results.
What is the essence of the tests? I wrote 4 functions that check in different ways whether a given part is sheet metal. And the method proposed by AlexB turned out to be the most stable in all situations. The remaining functions gave erroneous results in certain conditions.
Here is the text of the testing Module.

Code: Select all

Sub main()
Dim swApp As Object
Dim swModel As SldWorks.ModelDoc2
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

Debug.Print "IsSheetMetalByGetBendState      = " & IsSheetMetalByGetBendState(swModel)
Debug.Print "IsSheetMetalByBodies            = " & IsSheetMetalByBodies(swModel)
Debug.Print "IsSheetMetalByFeatTypeName      = " & IsSheetMetalByFeatTypeName(swModel)
Debug.Print "IsSheetMetalByFlatPatternFolder = " & IsSheetMetalByFlatPatternFolder(swModel)


End Sub


'#1
Function IsSheetMetalByGetBendState(ByVal swModel As SldWorks.ModelDoc2) As Boolean
    Dim nBendState As Long
    nBendState = swModel.GetBendState
    If nBendState <> swSMBendStateNone Then 'swSMBendStateNone = 0
        IsSheetMetalByGetBendState = True
        Exit Function
    End If
    IsSheetMetalByGetBendState = False
End Function


'#2
Function IsSheetMetalByBodies(ByVal TheModel As SldWorks.ModelDoc2) As Boolean
    Dim swPart As SldWorks.PartDoc
    Set swPart = TheModel

    If Not swPart Is Nothing Then
        Dim swBodies As Variant
        swBodies = swPart.GetBodies2(swBodyType_e.swSolidBody, False)

        If Not IsEmpty(swBodies) Then
            Dim i As Integer
            For i = 0 To UBound(swBodies)
                Dim swBody As SldWorks.Body2
                Set swBody = swBodies(i)
                If Not swBody Is Nothing Then
                    If swBody.IsSheetMetal() Then
                        IsSheetMetalByBodies = True
                        Exit Function
                    End If
                End If
            Next
        End If
    End If

    IsSheetMetalByBodies = False
End Function


'#3
Function IsSheetMetalByFeatTypeName(ByVal TheModel As SldWorks.ModelDoc2) As Boolean
    Dim swFeat As SldWorks.Feature
    IsSheetMetalByFeatTypeName = False
    Set swFeat = TheModel.FirstFeature
    Do While Not swFeat Is Nothing
         If UCase(swFeat.GetTypeName2()) = "SHEETMETAL" Then
             IsSheetMetalByFeatTypeName = True
             Exit Do
         End If
    Set swFeat = swFeat.GetNextFeature
    Loop
End Function

'#4
Function IsSheetMetalByFlatPatternFolder(ByVal prtModel As SldWorks.ModelDoc2) As Boolean
     Dim featureMgr As SldWorks.FeatureManager
     Dim flatPatternFolder As SldWorks.flatPatternFolder
     Dim FlatPatternsArray As Variant
     Set featureMgr = prtModel.FeatureManager
     If Not featureMgr Is Nothing Then
        Set flatPatternFolder = featureMgr.GetFlatPatternFolder
        If Not flatPatternFolder Is Nothing Then
            FlatPatternsArray = flatPatternFolder.GetFlatPatterns
            If Not IsEmpty(FlatPatternsArray) Then
                IsSheetMetalByFlatPatternFolder = True
                Exit Function
            End If
        End If
    End If
    IsSheetMetalByFlatPatternFolder = False
End Function
Here are the results from Window Immediate with different initial states of the sheet metal part.

Code: Select all

Just a part with one sheet body
	IsSheetMetalByGetBendState      = True
	IsSheetMetalByBodies            = True
	IsSheetMetalByFeatTypeName      = True
	IsSheetMetalByFlatPatternFolder = True
A part with one sheet body from which the body has been removed (delete body command)
	IsSheetMetalByGetBendState      = False
	IsSheetMetalByBodies            = False
	IsSheetMetalByFeatTypeName      = True 		'gives the wrong value
	IsSheetMetalByFlatPatternFolder = False
A part that has a Sheet Metal folder but does not have any Features for creating bodies (an empty tree down)
	IsSheetMetalByGetBendState      = False
	IsSheetMetalByBodies            = False
	IsSheetMetalByFeatTypeName      = False
	IsSheetMetalByFlatPatternFolder = False
Tree of part which was rolled above the Sheet metal folder
	IsSheetMetalByGetBendState      = True
	IsSheetMetalByBodies            = False  	'gives the wrong value
	IsSheetMetalByFeatTypeName      = True
	IsSheetMetalByFlatPatternFolder = False		'gives the wrong value

'Method #1 is best
User avatar
mihkov
Posts: 44
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 17
x 24

Re: How do I know that you are FLAT-PATTERN?

Unread post by mihkov »

One more question about sheet metal and VBA API deployments. What is the best way to programmatically get the dependent configuration with the FLAT-PATTERN state of the sheet metal part. I have a method that works, but it is very crude, based on passing incorrect parameters to an old function that throws an error - but does what it needs to do.

The point is that I apply the function ExportFlatPatternView to the ModelDoc2 Sheet Metal Parts object:
which checks sheet metal part is enabled to be saved in its flattened state to a DXF/DWG file at the specified path and filename.
conf.jpg
conf.jpg (5.2 KiB) Viewed 2400 times
However, I give her a path to nowhere and even with “forbidden characters” so that she definitely won’t succeed. As expected, it throws an error (which I suppress), but as a result we have a created dependent configuration to the active one, and with Sheet Metal FLAT-PATTERN.
Here is the code for my version:

Code: Select all

Sub main()
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
MakeFlatPatternSubconfForActiveCong swModel
End Sub

Sub MakeFlatPatternSubconfForActiveCong(sheetMetalModel As SldWorks.ModelDoc2)
	On Error Resume Next
	sheetMetalModel.ExportFlatPatternView "X:\just_for_make_SM-FLAT-PATTERN_config<>?|", 0
	On Error GoTo 0
End Sub
There is one line here - the result of stability. Can anyone suggest a more correct option?
Post Reply