static void Main(string[] args)
        {
            //Console.WriteLine("Starting..");

            //var swApp = new SldWorks();

            //var swAPIWarnings = 0;
            //var swAPIErrors = 0;

            //var importData = (ImportStepData)swApp.GetImportFileData(args[0]);
            //var swActiveDoc = (ModelDoc2)swApp.LoadFile4(args[0], "r", importData, swAPIErrors);

            //if (swActiveDoc == null)
            //{
            //    Console.WriteLine("swActiveDoc nullo " + swAPIWarnings + " " + swAPIErrors);
            //    return;
            //}

            //var swAssemblyDoc = (AssemblyDoc)swActiveDoc;

            //var SwConfiguration = swActiveDoc.GetActiveConfiguration();
            //var rootComponent = (Component2)SwConfiguration.GetRootComponent();
            //var fileName = rootComponent.Name2;

            //Console.WriteLine("Modello attivato " + fileName);

            //var transforation =
            //    (Array)AssemblyTraverse.KL_GetTransformsOfAssemblyComponents(rootComponent, swApp);
            //const string fatherName = KLgraph.RootLabel;
            //var pathComponent = rootComponent.Name2;
            //var componentName = pathComponent.Split('/').Last();
            //var componentPath = rootComponent.GetPathName();
            //var id = rootComponent.GetHashCode();

            //var childrenNumber = rootComponent.IGetChildrenCount();
            //var shape = AssemblyTraverse.KL_GetShapeAssembly(rootComponent, swApp);
            //var statistic = AssemblyTraverse.KL_GetStatisticAssembly(rootComponent, swApp);
            //var nodeAssembly = new KLgraph.KLnodeAssembly(id, transforation, fatherName, -1, rootComponent, pathComponent, componentName, componentPath, -1,
            //    childrenNumber, statistic, shape);

            //Console.WriteLine("Creato il nodo assemblato");

            //var vertexList = new List<KLgraph.KLnode>();
            //var edgeList = new List<KLgraph.KLedge>();
            //vertexList.Add(nodeAssembly);
            //var listOfMyListOfInstances = new List<MyListOfInstances>();

            //AssemblyTraverse.KL_GetGraphOfAssemblyComponents(rootComponent, nodeAssembly,
            //   ref vertexList, edgeList, ref listOfMyListOfInstances, swApp);


            //var partList = new List<KLgraph.KLnode>(vertexList);
            //partList.RemoveAll(v => v.GetType() != typeof(KLgraph.KLnodePart));

            //nodeAssembly.KLstatistic.PrincipalPartNumber = partList.Count();

            //nodeAssembly.Instances.AddRange(listOfMyListOfInstances);


            List <MyPatternOfComponents> listPattern  = new List <MyPatternOfComponents>();
            List <MyPatternOfComponents> listPattern2 = new List <MyPatternOfComponents>();
            StringBuilder fileOutput = new StringBuilder();

            fileOutput.AppendLine("RICERCA PATH");

            var compName           = "DIN";
            var listCentroidWordRF = new List <MyVertex>();
            var v1 = new MyVertex(-0.00302203528372547, -0.0657609306477435, 0.0657609306501929);
            var v2 = new MyVertex(-0.00302203528372547, 0.0930000000026054, 0.0);
            var v3 = new MyVertex(-0.00302203528372547, 0.0, -0.093000000000156);
            var v4 = new MyVertex(-0.00302203528372547, -0.0657609306477435, -0.0657609306505048);
            var v5 = new MyVertex(-0.00302203528372547, -0.0929999999973946, -0.0);
            var v6 = new MyVertex(-0.00302203528372547, 0.0657609306529544, 0.0657609306501929);
            var v7 = new MyVertex(-0.00302203528372547, 0.0657609306529543, -0.0657609306505049);
            var v8 = new MyVertex(-0.00302203528372547, 0.0, 0.0929999999998441);

            listCentroidWordRF.Add(v1);
            listCentroidWordRF.Add(v2);
            listCentroidWordRF.Add(v3);
            listCentroidWordRF.Add(v4);
            listCentroidWordRF.Add(v5);
            listCentroidWordRF.Add(v6);
            listCentroidWordRF.Add(v7);
            listCentroidWordRF.Add(v8);


            var newListOfComponetsNoInfo = new List <MyRepeatedComponent>();

            foreach (var comp in listCentroidWordRF)
            {
                var newComp = new MyRepeatedComponent();
                var newRE   = new MyRepeatedEntity();
                newRE.centroid = comp;
                newComp.Origin = comp;
                newComp.Name   = compName;
                newComp.IdCorrespondingNode = 0;
                newComp.RepeatedEntity      = newRE;


                newListOfComponetsNoInfo.Add(newComp);
            }


            AssemblyPatterns.KLFindPatternsOfComponents(newListOfComponetsNoInfo, listCentroidWordRF,
                                                        ref listPattern, ref listPattern2, ref fileOutput);
            //PatternComputationFunctions.GetAssemblyPatternsOfRepeatedElements(listOfMyListOfInstances, nodeAssembly,
            //    out listPattern, out listPattern2);


            //Console.WriteLine("Istanze trovate " + listOfMyListOfInstances.Count);
            //foreach (var inst in listOfMyListOfInstances)
            //{
            //    Console.WriteLine("Istanza di " + inst.Name + " trovata " + inst.ListOfMyComponent.Count + " volte");
            //}

            Console.WriteLine("Pattern trovati " + listPattern.Count);
            foreach (var patt in listPattern)
            {
                Console.WriteLine("Pattern di tipo " + patt.typeOfMyPattern + " formato da " + patt.listOfMyRCOfMyPattern.First().Name);
            }


            //Console.WriteLine("Ending.. nodi " + graph.VertexCount + " e archi " + graph.EdgeCount);

            /*
             * var fileName = Path.GetFileNameWithoutExtension(args[0]);
             * var storageGraph = new KLgraph.Graph(graph.Vertices.ToList(), graph.Edges.ToList());
             *
             * JsonSerializerSettings setting = new JsonSerializerSettings
             * {
             *  TypeNameHandling = TypeNameHandling.All,
             *  TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
             * };
             *
             * var directoryOutput = Path.GetDirectoryName(args[1]);
             * var fileNameOutput = Path.GetFileNameWithoutExtension(args[1]);
             * //Console.WriteLine(directoryOutput);
             * //Console.WriteLine(fileNameOutput);
             *
             * SaveModel(JsonConvert.SerializeObject(storageGraph, setting), directoryOutput, fileNameOutput + ".json");
             * Console.WriteLine("JSON saved!");
             */
            //swApp.ExitApp();
        }
示例#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);
        }