public static void GetAssemblyPatternsOfRepeatedElements(List <MyListOfInstances> listOfMyListOfInstances,
                                                                 KLgraph.KLnodeAssembly nodeAssembly, out List <MyPatternOfComponents> listPattern,
                                                                 out List <MyPatternOfComponents> listPattern2)
        {
            listPattern  = new List <MyPatternOfComponents>();
            listPattern2 = new List <MyPatternOfComponents>();
            AssemblyTraverse.LCComputeRepeatedPattern(listOfMyListOfInstances, ref listPattern, ref listPattern2,
                                                      null, null);
            nodeAssembly.KLlistPattern.AddRange(listPattern);
            nodeAssembly.KLlistPatternTwo.AddRange(listPattern2);

            foreach (MyPatternOfComponents pattern in listPattern2)
            {
                if (pattern.typeOfMyPattern == "linear TRANSLATION" ||
                    pattern.typeOfMyPattern == "TRANSLATION of length 2")
                {
                    nodeAssembly.KLstatistic.LinearPatternNumber++;
                }
                else if (pattern.typeOfMyPattern == "linear ROTATION")
                {
                    nodeAssembly.KLstatistic.CircularPatternNumber++;
                }
                else if (pattern.typeOfMyPattern == "circular TRANSLATION")
                {
                    nodeAssembly.KLstatistic.CircularPatternNumber++;
                }
                else if (pattern.typeOfMyPattern == "REFLECTION")
                {
                    nodeAssembly.KLstatistic.ReflectivePatternNumber++;
                }
            }

            foreach (MyPatternOfComponents myPatternOfComponentse in listPattern)
            {
                if (myPatternOfComponentse.typeOfMyPattern == "ROTATION")
                {
                    if (Math.Abs(myPatternOfComponentse.angle + 1) < 0.01)
                    {
                        nodeAssembly.KLstatistic.ReflectivePatternNumber++;
                    }
                    else
                    {
                        nodeAssembly.KLstatistic.CircularPatternNumber++;
                    }
                }
                else if (myPatternOfComponentse.typeOfMyPattern == "REFLECTION")
                {
                    nodeAssembly.KLstatistic.ReflectivePatternNumber++;
                }
                else
                {
                    nodeAssembly.KLstatistic.LinearPatternNumber++;
                }
            }
        }
Ejemplo n.º 2
0
        public static MyRepeatedComponent ComputeNewRepeatedComponent(SldWorks swApplication, Component2 component2, int idCorrespondingNode,
                                                                      MyTransformMatrix newRelativeTransformMatrix, int indexRepEntity, bool newIsLeaf)
        {
            // Aggiunto calcolo dell'entità ripetuta alla parte.
            var currentModel = component2.GetModelDoc2();
            var entityList   =
                (List <Entity>)AssemblyTraverse.KL_GetPartFaces(currentModel, component2.Name2,
                                                                swApplication);

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

            //swApplication.SendMsgToUser("Calcolo repeated entity di " + component2.Name2 + "\nnumero facce " + entityList.Count);
            MyRepeatedEntity newRepeatedEntity = ExtractInfoFromBRep.KLBuildRepeatedEntity(
                entityList, indexRepEntity, compositionMatrixOfComponentPart, swApplication);
            //swApplication.SendMsgToUser("Ha " + newRepeatedEntity.listOfVertices.Count + " vertici\n" + newRepeatedEntity.listOfAddedVertices.Count + " vertici aggiunti");

            // Fine calcolo entità rip da aggiungere alla componente.
            var newMyComponent = new MyRepeatedComponent(component2, idCorrespondingNode, newRelativeTransformMatrix, newIsLeaf,
                                                         newRepeatedEntity);

            return(newMyComponent);
        }
Ejemplo n.º 3
0
        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("Model " + fileName + " loaded correctly");

            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;
            List <MyPatternOfComponents> listPattern2;


            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 " + 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());

            var storageGraph = new List <List <MyPatternOfComponents> >();

            storageGraph.Add(listPattern2);
            storageGraph.Add(listPattern);

            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();
        }
Ejemplo n.º 4
0
        //This function takes a component and decomposes it in all the existing children.
        //For every children a MyTransformMatrix is computed. The computed transform matrix refers
        //to the given children component respect to its position in the original file
        //(such a matrix is obtained composing the MyTransformMatrix referred to the component
        //position respect to its father and the father's MyTransformMatrix).
        //If a children has other children it decomposes it too.
        public static void TraversAssemblyComponents(Component2 swComponent2, MyTransformMatrix fatherTransformMatrix,
                                                     SldWorks swApplication)
        {
            //List of the lists of instances of the same file object:
            var ListOfMyListOfInstances = new List <MyListOfInstances>();

            if (swComponent2 == null)
            {
                return;
            }

            var nameFile = "ClassifyComponents.txt";

            //I take the children of the root component
            var swChildrenComponent = (Array)swComponent2.GetChildren();

            int i = 0;

            foreach (Component2 component2 in swChildrenComponent)
            {
                //for each son I create a new ComputeNewRepeatedComponent
                var newRelativeTransformMatrix = AssemblyTraverse.GetTransformMatrix(component2, swApplication);
                var newTransformMatrix         = fatherTransformMatrix.ComposeTwoTransformMatrix(newRelativeTransformMatrix,
                                                                                                 swApplication);
                bool newIsLeaf = component2.IGetChildrenCount() == 0;
                //If the current son is not a "leaf" component the function is recalled again:
                if (component2.IGetChildrenCount() != 0)
                {
                    TraversAssemblyComponents(component2, newTransformMatrix, swApplication);
                }
                else
                {
                    var newMyComponent = ComputeNewRepeatedComponent(swApplication, component2, 0, newRelativeTransformMatrix, i, newIsLeaf);

                    //I verify if the list corresponding to this component already exists:
                    //KLdebug.Print("-Componente # " + indexRepEntity + " di " + swComponent2.Name2, nameFile);
                    string namePath          = component2.GetPathName();
                    string nameFileComponent = namePath.Split('\\').Last(); //to get the last name after the last "\"
                    //namePath = namePath.TrimEnd('-');
                    //string nameFileComponent = namePath.Remove((namePath.LastIndexOf('-') + 1));


                    //KLdebug.Print("   --->> namePath: " + namePath, "nomi.txt");
                    //KLdebug.Print("   --->> nameFileComponent: " + nameFileComponent, "nomi.txt");
                    //KLdebug.Print("     Matrice relativa", nameFile);
                    //KLdebug.PrintTransformMatrix(newRelativeTransformMatrix, nameFile, swApplication);
                    //KLdebug.Print("     Matrice assoluta", nameFile);
                    //KLdebug.PrintTransformMatrix(newTransformMatrix, nameFile, swApplication);


                    var indexOfFind = ListOfMyListOfInstances.FindIndex(list => list.Name == nameFileComponent);
                    if (indexOfFind != -1)
                    {
                        //The list referred to this component already exists. I add it to the corresponding list
                        //var newMyComponent = new ComputeNewRepeatedComponent();

                        //var newIndex =
                        //    ListOfMyListOfInstances[indexOfFind].ListOfMyComponent.Count + 1;
                        //newMyComponent.RepeatedEntity.idRE = newIndex;
                        ListOfMyListOfInstances[indexOfFind].ListOfMyComponent.Add(newMyComponent);
                        //KLdebug.Print("       AGGIORNATA LISTA ESISTENTE: " + newMyComponent.Name + " con id" +newMyComponent.RepeatedEntity.idRE, nameFile);
                    }

                    //else
                    //{
                    //    //Check of the component satisfy other shape criteria (volume and percentage of type of surfaces)
                    //    var shapeComparison = AssemblyTraverse.KL_GetShapePart(component2, swApplication);
                    //    var statisticComparison = AssemblyTraverse.KL_GetStatisticPart(component2,
                    //        shapeComparison.Surface, swApplication);

                    //    var addCompForShape = false;
                    //    foreach (MyListOfInstances instance in ListOfMyListOfInstances)
                    //    {
                    //        var component = (Component2)instance.ListOfMyComponent.First().Component;
                    //        var shapeOriginal = AssemblyTraverse.KL_GetShapePart(component, swApplication);
                    //        var statisticOriginal = AssemblyTraverse.KL_GetStatisticPart(component,
                    //            shapeOriginal.Surface,
                    //            swApplication);

                    //        if (KLcriteriaCheck.Size.Volume(shapeOriginal, shapeComparison, swApplication))
                    //        {
                    //            if (KLcriteriaCheck.Size.PercentageSurfacesType(statisticOriginal, statisticComparison,
                    //                swApplication))
                    //            {
                    //                namePath = component.GetPathName();
                    //                nameFileComponent = namePath.Split('\\').Last();
                    //                //nameFileComponent = namePath;
                    //                indexOfFind =
                    //                    ListOfMyListOfInstances.FindIndex(list => list.Name == nameFileComponent);
                    //                ListOfMyListOfInstances[indexOfFind].ListOfMyComponent.Add(newMyComponent);
                    //                addCompForShape = true;
                    //                break;
                    //            }
                    //        }
                    //    }

                    //    //The list referred to this component does not exist yet. I create it
                    //    if (!addCompForShape)
                    //    {
                    //        List<MyRepeatedComponent> newListOfComponentsOfListOfInstances = new List
                    //            <MyRepeatedComponent>
                    //        {
                    //            newMyComponent
                    //        };
                    //        var newListOfInstances = new MyListOfInstances(nameFileComponent,
                    //            newListOfComponentsOfListOfInstances);
                    //        ListOfMyListOfInstances.Add(newListOfInstances);
                    //        //KLdebug.Print("       CREATA NUOVA LISTA nome:" + newListOfInstances.Name, nameFile);
                    //    }
                    //}


                    i++;
                }
            }
        }