1 Reply Latest reply on Nov 20, 2017 3:43 AM by Ross Hazelwood

    Dangling References API Heal

    Robert Pancoast

      Greetings All!

       

      Consider When working with a Large model composed of ~7000 sub-models.

      Now let us assume that Rebuild operation failure exists on the Root of the Model [MechE call it 'top-level' asm].

       

      I have noticed a strong pattern: Dangling references in sketches of sub-models that prevent Rebuild operation.

       

      We need to continue parsing through the entire tree using a recursive algorithm, but also heal the ~7000 model's Dangling References.

              /// <summary>
              /// The create tree method.
              /// </summary>
              private void CreateTree()
              {
                  var solidWorksModel = this.solidWorksApplicationInterface.ActiveDoc as ModelDoc2;
                  if (solidWorksModel == null)
                  {
                      this.Write("Failed to Parse Active Document" + Environment.NewLine, Color.Red);
                      return;
                  }
      
      
                  var dataModel = this.DataFromModel(solidWorksModel);
      
      
                  // Set Active Doc as root to tree
                  this.Write(
                      "Tree Root [Active Document]: " + solidWorksModel.GetPathName() + Environment.NewLine, 
                      Color.Beige);
                  this.root = new TreeNode<DataModel>(dataModel);
                  var componentType = solidWorksModel.GetType();
                  if (componentType == (int)swDocumentTypes_e.swDocASSEMBLY)
                  {
                      this.RecursiveParse(this.root, solidWorksModel);
                  }
      
      
                  // The addin **must call GC.Collect() twice** here in order to retrieve all managed code pointers 
                  try
                  {
                      Marshal.ReleaseComObject(solidWorksModel);
                  }
                  catch (ArgumentException argumentException)
                  {
                      ConsoleEx.Write("Exception: " + argumentException.Message + Environment.NewLine);
                  }
                  catch (NullReferenceException nullReferenceException)
                  {
                      ConsoleEx.Write("Exception: " + nullReferenceException.Message + Environment.NewLine);
                  }
      
      
                  Cast.ToNull(out solidWorksModel);
                  GC.Collect();
                  GC.WaitForPendingFinalizers();
                  GC.Collect();
                  GC.WaitForPendingFinalizers();
              }
      
              /// <summary>
              /// The method that recursively parses a model to build a tree of all
              /// its sub-components.
              /// </summary>
              /// <param name="trunk">
              /// The <paramref name="trunk"/> of the tree for the current iteration.
              /// </param>
              /// <param name="solidWorksModel">
              /// The SolidWorks model.
              /// </param>
              private void RecursiveParse(TreeNode<DataModel> trunk, ModelDoc2 solidWorksModel)
              {
                  // Assume trunk is always verified as an assembly doc
                  if (solidWorksModel == null)
                  {
                      this.Write("Failed to Parse Document" + Environment.NewLine, Color.Red);
                      return;
                  }
      
      
                  this.Write("Parsing Children of " + solidWorksModel.GetPathName() + Environment.NewLine);
      
      
                  // var solidWorksExtension = solidWorksModel.Extension;
                  var solidWorksAssembly = Cast.To<AssemblyDoc>(solidWorksModel);
                  try
                  {
                      if (solidWorksAssembly == null)
                      {
                          return;
                      }
      
      
                      var subComponents = (object[])solidWorksAssembly.GetComponents(true);
                      foreach (var iterator in subComponents)
                      {
                          var solidWorksComp = iterator as Component2;
                          if (solidWorksComp == null)
                          {
                              continue;
                          }
      
      
                          ModelDoc2 solidWorksChildModel = solidWorksComp.GetModelDoc2();
                          if (solidWorksChildModel == null)
                          {
                              continue;
                          }
      
      
                          this.Write("Found " + solidWorksChildModel.GetPathName() + Environment.NewLine);
      
      
                          // Create a list of acceptable prefixes and check the filename
                          var prefixList = new List<string> { "A-", "P-", "C-" };
                          var filename = Path.GetFileName(solidWorksChildModel.GetPathName());
      
      
                          /*
                          var includeFile = false;
      
      
                          foreach (var prefix in prefixList)
                          {
                              if (filename != null && filename.StartsWith(prefix))
                              {
                                  includeFile = true;
                              }
                          }
      
      
                          // If none of the prefixes are found, ignore the file and continue
                          if (!includeFile)
                          {
                              continue;
                          }
                          */
      
      
                          // Check for duplicates
                          var duplicate = false;
                          var child = this.DataFromModel(solidWorksChildModel);
                          foreach (var sibling in trunk.Children)
                          {
                              if (child.Path == sibling.Data.Path)
                              {
                                  duplicate = true;
                              }
                          }
      
      
                          if (duplicate)
                          {
                              continue;
                          }
      
      
                          // None, Part, Assembly or Drawing
                          var childType = solidWorksChildModel.GetType();
                          if (childType == (int)swDocumentTypes_e.swDocPART)
                          {
                              trunk.AddChild(child);
                              this.RecursivelyLooped(solidWorksChildModel);
                          }
                          else if (childType == (int)swDocumentTypes_e.swDocASSEMBLY)
                          {
                              var branch = trunk.AddChild(child);
                              this.RecursiveParse(branch, solidWorksChildModel);
                              this.RecursivelyLooped(solidWorksChildModel);
                          }
                      }
                  }
                  catch (CatchAllException catchAllException)
                  {
                      ConsoleEx.Write("Exception: " + catchAllException.Message + Environment.NewLine);
                  }
              }
      
      
              private void RecursivelyLooped(ModelDoc2 solidWorksChildModel)
              {
                  // TODO: FOR ALL PART OF ASM
                  // DO HEAL
      
      
                  // None, Part, Assembly or Drawing
                  var childType = solidWorksChildModel.GetType();
                  if (childType == (int)swDocumentTypes_e.swDocPART)
                  {
                      var filename = Path.GetFileName(solidWorksChildModel.GetPathName());
                      Log.TextEntry("Part: " + filename);
      
      
                  }
                  else if (childType == (int)swDocumentTypes_e.swDocASSEMBLY)
                  {
                      var filename = Path.GetFileName(solidWorksChildModel.GetPathName());
                      Log.TextEntry("Assembly: " + filename);
      
      
                  }
                  solidWorksChildModel.ForceRebuild3(false);
                  solidWorksChildModel.ShowNamedView2("Isometric", 7);
                  solidWorksChildModel.ViewZoomtofit2();
                  if (solidWorksChildModel.GetSaveFlag() == true)
                  {
                      int errors = 0;
                      int warnings = 0;
                      solidWorksChildModel.Save3((int)swSaveAsOptions_e.swSaveAsOptions_Silent,
                          ref errors, ref warnings);
                  }
              }
      

       

      Thanks,

      Rob