3 Replies Latest reply on May 26, 2017 1:44 PM by Amen Allah Jlili

    traverse assembly and keep the order (c#)

    Julien Losier

      Hello there,

      I have an assembly that is export to create a BOM but the order is wrong in so many level.

      I saw the answer for please help(traverse assembly) with someone saying " If you do mind the order of the tree you will have to traverse on the features tree and look for components. The API help is not explaining that so let me know if you need help."

      I need your help please.

      Basically I have :

       

      here I load my active doc to get my assembly/part

         public override BomNode LoadItemTree()

              {

                  TraceEnterMethod();

       

       

                  var doc = (ModelDoc2)_swApp.ActiveDoc;

                  if (doc == null) return null;

                  var selMgr = (ISelectionMgr)doc.SelectionManager;

                  var obj = selMgr.GetSelectedObject6(1,-1);

                  object objToProcess=null;

                  SWDocument swdoc = null;

                   (some validation)

                  swdoc = new SWDocument(doc, (IComponent2)objToProcess, configName);

       

      public SWDocument(IModelDoc2 openDocument, IComponent2 selectedObject, string configurationName = null)

              {

                  _errorsLoading = new List<string>();

                  IModelDoc2 model;

                  if (selectedObject == null)

                      model = openDocument;

                  else if ((IModelDoc2)((Component2)selectedObject).GetModelDoc() == null)

       

                      model = openDocument;

                  else

                      model = (IModelDoc2)((Component2)selectedObject).GetModelDoc();

                (some code)

       

                else if (model is AssemblyDoc)

                  {               

                       var asmRootDoc = (AssemblyDoc)model;

                      if(CadSettingsController.Instance.GlobalSettings.SolidworksSettings.ResolveLightweightAssemblies)

                          asmRootDoc.ResolveAllLightWeightComponents(true);

                      var rootAssembly = (Component2)docConfig.GetRootComponent3(true);            

                      var assembly = new Assembly(0, (AssemblyDoc)model, docConfig, rootAssembly, null);

                      LoadAssemblyParts(assembly);

                      LoadSubAssemblies(assembly);

                      _rootItem = assembly;

                  }

       

              private static void LoadAssemblyParts(Assembly assembly)

              {

                  var lastLoadedPart = string.Empty;

                  var childs = (object[])assembly.SwComponent.GetChildren();

                  var childCount = childs.Length;

                  for (var i = 0; i < childCount; i++)

                  {

                      var aChild = (Component2)childs[i];

                      if (aChild.IsSuppressed() || aChild.ExcludeFromBOM)

                          continue;

       

       

                      var itemDoc = (ModelDoc2)aChild.GetModelDoc2();

       

       

                      if (itemDoc == null)

                      {

                          _errorsLoading.Add($"Parent Item = {assembly.Name} - Last element = {lastLoadedPart}");

                          continue;

                      }

       

       

                      var docConfig = (Configuration)itemDoc.GetConfigurationByName(aChild.ReferencedConfiguration);

       

       

                      if (!(itemDoc is PartDoc))

                          continue;

       

       

                      var part = new Part(assembly.Level + 1, (PartDoc)itemDoc, docConfig, aChild, assembly);

                      if (assembly.AddChild(part))

                          LoadProfilesAndWeldments(part);

                      lastLoadedPart = part.Name;

                  }

              }

       

       

      I saw 2014 SOLIDWORKS API Help - Traverse Features By Reverse Position Example (C#)  but don't know how to apply or use it to get the order and the parts.

      Thank you for your help.