public static void MainPatternSearch_Part(bool EntirePart, ref List <MyGroupingSurface> listOfMyGroupingSurface,
                                                  List <MyGroupingSurface> listOfInitialGroupingSurface, ref StringBuilder fileOutput, SldWorks mySwApplication)
        {
            var listOfOutputPattern    = new List <MyPattern>(); //list of output patterns found
            var listOfOutputPatternTwo = new List <MyPattern>(); //list of output patterns of length 2 found
            var toleranceOK            = true;                   //it is TRUE if the tolerance level is OK during the paths searching, FALSE otherwise

            while (listOfMyGroupingSurface.Count > 0 && toleranceOK == true)
            {
                MyGroupingSurface currentGroupingSurface;
                if (!EntirePart)
                {
                    currentGroupingSurface =
                        new MyGroupingSurface(listOfMyGroupingSurface[0].groupingSurface,
                                              listOfMyGroupingSurface[0].listOfREOfGS);
                }
                else
                {
                    currentGroupingSurface = new MyGroupingSurface(listOfMyGroupingSurface[0].KLplanareSurface,
                                                                   listOfMyGroupingSurface[0].listOfREOfGS);
                }

                listOfMyGroupingSurface.RemoveAt(0);
                fileOutput.AppendLine(" ");
                fileOutput.AppendLine("(Al momento sono rimaste " + listOfMyGroupingSurface.Count +
                                      " superfici, compresa questa.)");

                PartUtilities.GeometryAnalysis.FindPatternsInGS(currentGroupingSurface, ref fileOutput,
                                                                ref listOfOutputPattern, ref listOfMyGroupingSurface,
                                                                ref listOfOutputPatternTwo, ref toleranceOK, listOfInitialGroupingSurface);
            }

            //Assigning of id numbers to the found MyPattern
            //(the id will be used in composed pattern search phase)
            //At the same time, we draw the pattern centroids
            ModelDoc2 SwModel = mySwApplication.ActiveDoc;

            SwModel.ClearSelection2(true);
            SwModel.Insert3DSketch();
            var i = 0;

            foreach (var pattern in listOfOutputPattern)
            {
                pattern.idMyPattern = i;
                i++;
                var listOfCentroidsOfPattern = pattern.listOfMyREOfMyPattern.Select(re => re.centroid).ToList();
                var patternCentroid          = ExtractInfoFromBRep.computeCentroidsOfVertices(listOfCentroidsOfPattern);
                pattern.patternCentroid = patternCentroid;
                //SwModel.CreatePoint2(pattern.patternCentroid.x, pattern.patternCentroid.y, pattern.patternCentroid.z);
            }
            foreach (var pattern in listOfOutputPatternTwo)
            {
                pattern.idMyPattern = i;
                i++;
                var listOfCentroidsOfPattern = pattern.listOfMyREOfMyPattern.Select(re => re.centroid).ToList();
                var patternCentroid          = ExtractInfoFromBRep.computeCentroidsOfVertices(listOfCentroidsOfPattern);
                pattern.patternCentroid = patternCentroid;
                //SwModel.CreatePoint2(pattern.patternCentroid.x, pattern.patternCentroid.y, pattern.patternCentroid.z);
            }
            //SwModel.InsertSketch();

            //Patterns are subdivided in Line patterns and in Circle patterns:
            var listOfOutputPatternLine   = new List <MyPattern>();
            var listOfOutputPatternCircum = new List <MyPattern>();

            foreach (var pattern in listOfOutputPattern)
            {
                if (pattern.pathOfMyPattern.GetType() == typeof(MyLine))
                {
                    listOfOutputPatternLine.Add(pattern);
                }
                else
                {
                    listOfOutputPatternCircum.Add(pattern);
                }
            }

            //The results are shown giving color to the model:
            ColorFace.MyVisualOutput(listOfOutputPatternLine, mySwApplication);
            ColorFace.MyVisualOutput(listOfOutputPatternCircum, mySwApplication);

            //The same for patterns of length 2:

            ColorFace.MyVisualOutput(listOfOutputPatternTwo, mySwApplication);

            //Build the list of MyGroupingSurfaceForPatterns:
            var listOfGroupingSurfaceForPatterns = new List <MyGroupingSurfaceForPatterns>();

            if (!EntirePart)
            {
                foreach (var gs in listOfInitialGroupingSurface)
                {
                    var listOfPatternsLineForThisGS = listOfOutputPatternLine.FindAll(
                        pattern => pattern.listOfGroupingSurfaces.FindIndex(
                            surface => ExtractInfoFromBRep.MyEqualsSurface(surface, gs.groupingSurface, mySwApplication)) !=
                        -1);
                    var listOfPatternsTwoForThisGS = listOfOutputPatternTwo.FindAll(
                        pattern => pattern.listOfGroupingSurfaces.FindIndex(
                            surface => ExtractInfoFromBRep.MyEqualsSurface(surface, gs.groupingSurface, mySwApplication)) !=
                        -1);
                    listOfPatternsLineForThisGS.AddRange(listOfPatternsTwoForThisGS);

                    var listOfPatternsCircumForThisGS = listOfOutputPatternCircum.FindAll(
                        pattern => pattern.listOfGroupingSurfaces.FindIndex(
                            surface => ExtractInfoFromBRep.MyEqualsSurface(surface, gs.groupingSurface, mySwApplication)) !=
                        -1);
                    if (listOfPatternsLineForThisGS.Count > 1 || listOfPatternsCircumForThisGS.Count > 1)
                    {
                        var newGSForPatterns = new MyGroupingSurfaceForPatterns(gs.groupingSurface,
                                                                                listOfPatternsLineForThisGS, listOfPatternsCircumForThisGS);
                        listOfGroupingSurfaceForPatterns.Add(newGSForPatterns);
                    }
                }
            }


            //>>>>>>>COMPOSED PATTERN SEARCH:
            List <MyComposedPattern> listOfOutputComposedPattern;
            List <MyComposedPattern> listOfOutputComposedPatternTwo;

            PartUtilities_ComposedPatterns.GeometryAnalysis.FindComposedPatterns(listOfGroupingSurfaceForPatterns, out listOfOutputComposedPattern,
                                                                                 out listOfOutputComposedPatternTwo, SwModel, mySwApplication, ref fileOutput);


            ColorFace.MyVisualOutput_ComposedPatterns(listOfOutputComposedPattern,
                                                      listOfOutputComposedPatternTwo, mySwApplication, SwModel);
        }
Пример #2
0
        public static void MainPatternSearch_Assembly(List <MyListOfInstances> ListOfMyListOfInstances,
                                                      ModelDoc2 SwModel, SldWorks swApplication, ref StringBuilder fileOutput,
                                                      out List <MyPatternOfComponents> listOfOutputPatternOut,
                                                      out List <MyPatternOfComponents> listOfOutputPatternTwoOut)
        {
            //ListOfMyListOfInstances.Clear();
            var identityTransformMatrix = new MyTransformMatrix();

            //Update ListOfMyListOfInstances:
            //-delete list containing only one occurrence
            //-reorder the list by decreasing ordering respect to the number of occurrences
            ListOfMyListOfInstances.RemoveAll(list => list.ListOfMyComponent.Count == 1);
            var numOfListOfInstances = ListOfMyListOfInstances.Count;

            ListOfMyListOfInstances = ListOfMyListOfInstances.OrderByDescending(x => x.ListOfMyComponent.Count).ToList();

            listOfOutputPatternOut    = new List <MyPatternOfComponents>(); //list of output patterns found
            listOfOutputPatternTwoOut = new List <MyPatternOfComponents>(); //list of output patterns of length 2 found
            //Console.WriteLine("Numero istanze " + ListOfMyListOfInstances.Count);

            for (var k = 0; k < numOfListOfInstances; k++)
            {
                //Console.WriteLine("Analizzo istanza: " + ListOfMyListOfInstances[k].Name + " ripetuta " + ListOfMyListOfInstances[k].ListOfMyComponent.Count);
                var listOfOutputPattern    = new List <MyPatternOfComponents>(); //list of output patterns found
                var listOfOutputPatternTwo = new List <MyPatternOfComponents>(); //list of output patterns of length 2 found

                var listOfComponents = new List <MyRepeatedComponent>(ListOfMyListOfInstances[k].ListOfMyComponent);
                //Verify if there exist coinciding origins: in that case that component is skipped
                //Otherwise the pexisting patterns are identified:
                //var listOfVertexOrigins = listOfComponents.Select(component2 => component2.Origin).ToList();

                //var found = false;
                //var indexRepEntity = 0;
                //while (indexRepEntity < listOfVertexOrigins.Count - 1 && found == false)
                //{
                //    if (listOfVertexOrigins.FindIndex(origin => origin.Equals(listOfVertexOrigins[indexRepEntity]) &&
                //                                                listOfVertexOrigins.IndexOf(origin) != indexRepEntity) != -1)
                //    {
                //        found = true;
                //    }
                //    else
                //    {
                //        indexRepEntity++;
                //    }
                //}

                //// Lo commento perché vorrei che non usasse indexRepEntity sistemi di riferimento per il riconoscimento, ma indexRepEntity vertici delle parti

                //if (found == false)
                //{
                //    AssemblyPatterns.FindPatternsOfComponents(listOfComponents, listOfVertexOrigins,
                //        ref listOfOutputPattern, ref listOfOutputPatternTwo, SwModel, swApplication, ref fileOutput);
                //}
                //else
                //{

                //    swApplication.SendMsgToUser("Component '" +
                //                                ListOfMyListOfInstances[k].Name + "' cannot be processed. SKIPPED.");
                //}

                // Modifica aggiunta da Katia per il controllo delle facce

                var listCentroidWordRF = new List <MyVertex>();
                //swApplication.SendMsgToUser("Analizzo " + ListOfMyListOfInstances[k].Name);
                //swApplication.SendMsgToUser("Nome istanza " + instance.Name);
                //swApplication.SendMsgToUser("Ho " + listOfComponents.Count + " componenti");

                foreach (MyRepeatedComponent repeatedComponent in listOfComponents)
                {
                    var sameIndex = listOfComponents.IndexOf(repeatedComponent);
                    repeatedComponent.RepeatedEntity.idRE = sameIndex;

                    var newRepeatedEntity = repeatedComponent.RepeatedEntity;

                    if (newRepeatedEntity.listOfFaces.Any() && !repeatedComponent.IsSphere)
                    {
                        //swApplication.SendMsgToUser("Ci sono facce");

                        double[] centroidAffine =
                        {
                            newRepeatedEntity.centroid.x, newRepeatedEntity.centroid.y,
                            newRepeatedEntity.centroid.z, 1
                        };

                        double scaleFactor = 1;

                        double[,] compositionMatrixOfComponentPart =
                            new double[4, 4]
                        {
                            {
                                (double)repeatedComponent.Transform.RotationMatrix[0, 0],
                                (double)repeatedComponent.Transform.RotationMatrix[0, 1],
                                (double)repeatedComponent.Transform.RotationMatrix[0, 2],
                                (double)repeatedComponent.Transform.TranslationVector[0]
                            },
                            {
                                (double)repeatedComponent.Transform.RotationMatrix[1, 0],
                                (double)repeatedComponent.Transform.RotationMatrix[1, 1],
                                (double)repeatedComponent.Transform.RotationMatrix[1, 2],
                                (double)repeatedComponent.Transform.TranslationVector[1]
                            },
                            {
                                (double)repeatedComponent.Transform.RotationMatrix[2, 0],
                                (double)repeatedComponent.Transform.RotationMatrix[2, 1],
                                (double)repeatedComponent.Transform.RotationMatrix[2, 2],
                                (double)repeatedComponent.Transform.TranslationVector[2]
                            },
                            { 0.0, 0.0, 0.0, 1 }
                        };

                        var newCentroid = Matrix.Multiply(compositionMatrixOfComponentPart, centroidAffine);

                        var centroid = new MyVertex(newCentroid[0], newCentroid[1], newCentroid[2]);
                        // Ho dovuto cambiare l'origine perché il find pattern a volte prende indexRepEntity centri dalla lista in input a volte lo estrae dalla repeated component.
                        //repeatedComponent.Origin.x = newCentroid[0];
                        //repeatedComponent.Origin.y = newCentroid[1];
                        //repeatedComponent.Origin.z = newCentroid[2];
                        //listCentroidWordRF.Add(centroid);

                        repeatedComponent.Origin.x = newRepeatedEntity.centroid.x;
                        repeatedComponent.Origin.y = newRepeatedEntity.centroid.y;
                        repeatedComponent.Origin.z = newRepeatedEntity.centroid.z;
                        listCentroidWordRF.Add(newRepeatedEntity.centroid);

                        //SwModel = swApplication.ActiveDoc;
                        //SwModel.ClearSelection2(true);
                        //SwModel.Insert3DSketch();
                        //SwModel.CreatePoint2(newRepeatedEntity.centroid.x, newRepeatedEntity.centroid.y,
                        //    newRepeatedEntity.centroid.z);
                        //SwModel.InsertSketch();

                        // listOfOutputPattern.Clear();
                        // listOfOutputPatternTwo.Clear();
                    }
                    // Se non ci sono facce verifico se è tutta freeform e uguale ad una sfera
                    else if (repeatedComponent.IsSphere)
                    {
                        // Calcolo del centro di massa per trovare il pattern
                        var body = (Body2)repeatedComponent.Component.GetBody();
                        var mass = (double[])body.GetMassProperties(0);

                        var centerOfMass = new MyVertex(mass[0], mass[1], mass[2]);
                        repeatedComponent.Origin.x = centerOfMass.x;
                        repeatedComponent.Origin.y = centerOfMass.y;
                        repeatedComponent.Origin.z = centerOfMass.z;
                        listCentroidWordRF.Add(centerOfMass);

                        //SwModel = swApplication.ActiveDoc;
                        //SwModel.ClearSelection2(true);
                        //SwModel.Insert3DSketch();
                        //SwModel.CreatePoint2(mass[0], mass[1], mass[2]);
                        //SwModel.InsertSketch();
                    }
                }
                //swApplication.SendMsgToUser("Calcolo pattern " + listOfComponents[0].Name + "\ncomponenti " + listOfComponents.Count);
                if (listCentroidWordRF.Count == listOfComponents.Count)
                {
                    AssemblyPatterns.KLFindPatternsOfComponents(listOfComponents, listCentroidWordRF,
                                                                ref listOfOutputPattern, ref listOfOutputPatternTwo, SwModel, swApplication, ref fileOutput);
                }
                if (listOfOutputPattern.Any())
                {
                    foreach (var patternOfComponents in listOfOutputPattern)
                    {
                        listOfOutputPatternOut.Add(patternOfComponents);
                        //swApplication.SendMsgToUser("Patern " + patternOfComponentse.typeOfMyPattern + " lunghezza " + patternOfComponentse.listOfMyRCOfMyPattern.Count);
                        var listOfOriginsThisPattern =
                            patternOfComponents.listOfMyRCOfMyPattern.Select(rc => rc.Origin).ToList();
                        var patternCentroid =
                            ExtractInfoFromBRep.computeCentroidsOfVertices(listOfOriginsThisPattern);
                        patternOfComponents.patternCentroid = patternCentroid;
                    }
                }
                if (listOfOutputPatternTwo.Any())
                {
                    foreach (var patternOfComponentse in listOfOutputPatternTwo)
                    {
                        listOfOutputPatternTwoOut.Add(patternOfComponentse);
                        //swApplication.SendMsgToUser("Patern " + patternOfComponentse.typeOfMyPattern + " lunghezza " + patternOfComponentse.listOfMyRCOfMyPattern.Count);
                    }
                }
            }
            //}
            //It shows and print the detected patterns:
            //ShowAndPrintResults_Assembly(listOfOutputPatternOut, listOfOutputPatternTwoOut, SwModel, swApplication);

            ////Assigning of id numbers to the found MyPatternOfComponents
            ////(the id will be used in composed pattern search phase)
            ////At the same time, we draw the pattern origins
            //SwModel = swApplication.ActiveDoc;
            //SwModel.ClearSelection2(true);
            //SwModel.Insert3DSketch();
            //var j = 0;

            //foreach (var pattern in listOfOutputPattern)
            //{
            //    pattern.idMyPattern = j;
            //    j++;
            //    var listOfOriginsThisPattern =
            //        pattern.listOfMyRCOfMyPattern.Select(rc => rc.Origin).ToList();
            //    var patternCentroid =
            //        ExtractInfoFromBRep.computeCentroidsOfVertices(listOfOriginsThisPattern);
            //    pattern.patternCentroid = patternCentroid;
            //    SwModel.CreatePoint2(pattern.patternCentroid.x, pattern.patternCentroid.y,
            //        pattern.patternCentroid.z);
            //}
            //foreach (var pattern in listOfOutputPatternTwo)
            //{
            //    pattern.idMyPattern = j;
            //    j++;
            //    var listOfOriginsThisPattern =
            //        pattern.listOfMyRCOfMyPattern.Select(rc => rc.Origin).ToList();
            //    var patternCentroid =
            //        ExtractInfoFromBRep.computeCentroidsOfVertices(listOfOriginsThisPattern);
            //    pattern.patternCentroid = patternCentroid;
            //    SwModel.CreatePoint2(pattern.patternCentroid.x, pattern.patternCentroid.y,
            //        pattern.patternCentroid.z);
            //}
            //SwModel.InsertSketch();

            ////Patterns are subdivided in Line patterns and in Circle patterns:
            //var listOfOutputPatternLine = new List<MyPatternOfComponents>();
            //var listOfOutputPatternCircum = new List<MyPatternOfComponents>();
            //var nameFile = "ComposedPatterns_Assembly.txt";
            //foreach (var pattern in listOfOutputPattern)
            //{
            //    if (pattern.pathOfMyPattern.GetType() == typeof (MyLine))
            //    {
            //        listOfOutputPatternLine.Add(pattern);
            //        KLdebug.Print("- pattern di tipo " + pattern.typeOfMyPattern, nameFile);
            //        KLdebug.Print("  di lunghezza " + pattern.listOfMyRCOfMyPattern.Count, nameFile);
            //    }
            //    else
            //    {
            //        listOfOutputPatternCircum.Add(pattern);
            //        KLdebug.Print("- pattern di tipo " + pattern.typeOfMyPattern, nameFile);
            //        KLdebug.Print("  di lunghezza " + pattern.listOfMyRCOfMyPattern.Count, nameFile);
            //    }
            //}

            ////      >>>>>> COMPOSED PATTERN SEARCH:
            //List<MyComposedPatternOfComponents> listOfOutputComposedPattern;
            //List<MyComposedPatternOfComponents> listOfOutputComposedPatternTwo;
            //FindComposedPatternsOfComponents(
            //    listOfOutputPatternLine,
            //    listOfOutputPatternCircum,
            //    out listOfOutputComposedPattern,
            //    out listOfOutputComposedPatternTwo,
            //    SwModel, swApplication, ref fileOutput);

            //ShowAndPrintResults_Assembly_ComposedPatterns(listOfOutputComposedPattern,
            //    listOfOutputComposedPatternTwo,
            //    SwModel, swApplication);
        }