Dangling References API Heal

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.

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

Thanks,

Rob

SolidworksApi macros