static ModelDoc2 Convert2Part(ModelDoc2 assembly, Dictionary <string, Info> assemblyInfo) { // Used when comparing assemblies. ModelDoc2 model = assembly; ModelDocExtension modelExt = model.Extension; int errors = 0; int warnings = 0; string assemblyPartName; string assemblyPartPath; ModelDoc2 convertedAssembly; object[] bodies_array; bool check; if ((int)assemblyInfo["Type"].Value == (int)swDocumentTypes_e.swDocPART) { Logger.Error("VolumeComparator.cs", "Convert2Part()", "The document provided is not an assembly document."); return(null); } assemblyPartName = assemblyInfo["Name"].Value + "_PartConversion.sldprt"; assemblyPartPath = assemblyInfo["Folder"].Value + assemblyPartName; // Logger.Info("Saving Assembly as '.SLDPRT'..."); // Logger.Info("More Specifically: " + assemblyPartPath); swApp.ActivateDoc3((string)assemblyInfo["Title"].Value, true, (int)swRebuildOnActivation_e.swRebuildActiveDoc, ref errors); check = modelExt.SaveAs(assemblyPartPath, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, errors, warnings); if (errors != 0) { Logger.Error("VolumeComparator", "SaveAsPart", "swSaveAsError: " + errors); } else if (warnings != 0) { Logger.Warn("swFileSaveWarning: " + warnings); } else if (check) { // Logger.Info("Save successful."); } else { Logger.Error("VolumeComparator", "SaveAsPart", "Couldn't save for unknown reason."); } // CloseDocs(new object[] { assembly }); convertedAssembly = SwApp.OpenFile(assemblyPartPath); FeatureManager partFeatureMgr = convertedAssembly.FeatureManager; Feature partFeature; bodies_array = ((PartDoc)convertedAssembly).GetBodies2((int)swBodyType_e.swSolidBody, true); Body2[] bodies = new Body2[bodies_array.Length]; for (int i = 0; i < bodies_array.Length; i++) { bodies[i] = (Body2)bodies_array[i]; } // Logger.Info("Combining all bodies together..."); partFeature = partFeatureMgr.InsertCombineFeature((int)swBodyOperationType_e.SWBODYADD, null, bodies); if (partFeature == null) { Logger.Warn("No Body found to be touching eachother"); return(null); } convertedAssembly.Save(); return(convertedAssembly); }
static CompareResultStatus StartComparison(ModelDoc2 component1, ModelDoc2 component2) { AssemblyDoc swAsbly; int volumeDiff, faceDiff, areaDiff; double aMinusB, bMinusA, aAndB; object[] bodies; Body2 body1, body2; ModelDoc2 comparePart; int comparisonType; double volumeA, volumeB; VolumeCompareResultStatus result; bool cgAligned; lang = SwApp.GetLang(); Dictionary <string, Info> component1Info; Dictionary <string, Info> component2Info; component1Info = GetInfo(component1); component2Info = GetInfo(component2); comparisonType = CheckComponents(component1, component1Info, component2, component2Info); if (comparisonType == 1) { component1 = Convert2Part(component1, component1Info); component2 = Convert2Part(component2, component2Info); } Logger.Info("Comparing '" + component1Info["Name"].Value + "' with '" + component2Info["Name"].Value + "'"); Program.report.AddDelayedSubSection("'" + component1Info["Title"].Value + "' & '" + component2Info["Title"].Value + "'"); swAsbly = CreateAssembly(); InsertComponents(swAsbly, component1, component2); SaveAsPart(swAsbly, component1Info, component2Info); cgAligned = CheckCG(component1, component2); if (!cgAligned) { Logger.Warn("CG has moved by more than 10%"); Program.report.AddDelayedLine("CG has moved by more than 10%"); } // CloseDocs(new object[] { swAsbly, component1, component2 }); comparePart = SwApp.OpenFile(comparePartPath); bodies = ((PartDoc)comparePart).GetBodies2((int)swBodyType_e.swSolidBody, true); body1 = (Body2)bodies[0]; body2 = (Body2)bodies[1]; volumeA = (body1.GetMassProperties(0))[3]; volumeB = (body2.GetMassProperties(0))[3]; faceDiff = CompareFaces(body1, body2); areaDiff = CompareArea(body1, body2); volumeDiff = CompareVolume(body1, body2); Logger.Info("Volume Compare:\t" + volumeDiff); Logger.Info("Area Compare:\t" + areaDiff); Logger.Info("Faces Compare:\t" + faceDiff); Program.report.AddDelayedLine("Volume Compare:\t" + volumeDiff); Program.report.AddDelayedLine("Area Compare:\t" + areaDiff); Program.report.AddDelayedLine("Faces Compare:\t" + faceDiff); aMinusB = SubstractVolume(comparePart, body1, body2); Logger.Info("Volume A-B: " + aMinusB); Program.report.AddDelayedLine("Volume A-B:\t\t" + aMinusB); bMinusA = SubstractVolume(comparePart, body2, body1); Logger.Info("Volume B-A: " + aMinusB); Program.report.AddDelayedLine("Volume B-A:\t\t" + aMinusB); aAndB = CommonVolume(comparePart, body1, body2); Logger.Info("Volume B&A: " + aAndB); Program.report.AddDelayedLine("Volume B&A:\t\t" + aAndB); // CloseDocs(new object[] { comparePart }); result = casefinder(volumeA, volumeB, volumeDiff, faceDiff, areaDiff, aMinusB, bMinusA, aAndB); if (result == VolumeCompareResultStatus.Identical && cgAligned) { Logger.Info("Geometric result: Identical"); return(CompareResultStatus.Identical); } else if (!cgAligned) { Logger.Info("Geometric result: Similar"); return(CompareResultStatus.Similar); } else { Logger.Info("Geometric result: Different"); return(CompareResultStatus.Different); } }