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++;
                }
            }
        }
Exemple #2
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();
        }
        public static AdjacencyGraph <KLgraph.KLnode, KLgraph.KLedge> KL_GetGraphOfAssemblyComponents
            (Component2 fatherComponent, KLgraph.KLnode father, ref List <KLgraph.KLnode> listVertices, List <KLgraph.KLedge> listEdges,
            ref List <MyListOfInstances> listOfMyListOfInstances, SldWorks swApplication)
        {
            var graph = new AdjacencyGraph <KLgraph.KLnode, KLgraph.KLedge>(true);

            var fatherId = father.HashCode;

            //var fatherComponent = father.Comp;
            graph.AddVertexRange(listVertices);
            graph.AddEdgeRange(listEdges);
            {
                if (fatherComponent != null)
                {
                    var swChildrenComponent = (Array)fatherComponent.GetChildren();
                    var rootNode            = listVertices.FirstOrDefault(v => v.Name.Equals(father.Name));

                    int i = 0;
                    try
                    {
                        var index = 0;
                        //Console.WriteLine("Number of elements to analyze: " + swChildrenComponent.Length);
                        foreach (Component2 component in swChildrenComponent)
                        {
                            index++;
                            Console.WriteLine(String.Format("Processing component {0} over {1}", index, swChildrenComponent.Length));

                            //if (!component.Name2.Contains("Arbre satellite BV"))
                            {
                                var transforation =
                                    (Array)KL_GetTransformsOfAssemblyComponents(component, swApplication);
                                var    fatherName    = fatherComponent.Name2;
                                var    componentName = component.Name2;
                                object vBodyInfo;
                                var    bodyList =
                                    (Array)(component.GetBodies3((int)swBodyType_e.swAllBodies, out vBodyInfo));
                                //var componentName = pathComponent.Split('\\').Last();
                                var nameLastPart = componentName.TrimEnd('-');
                                nameLastPart = nameLastPart.Remove(nameLastPart.LastIndexOf('-') + 1);
                                var componentPath = component.GetPathName();
                                var hashCode      = component.GetHashCode();
                                //var nodeId = component.GetID();
                                var nodeId         = 0;
                                var childrenNumber = component.IGetChildrenCount();

                                if (childrenNumber == 0)
                                {
                                    //Console.WriteLine("Single");
                                    //var shape = KL_GetShapePart(component, swApplication);
                                    //var statistic = KL_GetStatisticPart(component, shape.Surface, swApplication);
                                    //swApplication.SendMsgToUser(componentName + "  " + statistic.Genus);
                                    var nodePart = new KLgraph.KLnodePart(hashCode, transforation, fatherName, fatherId,
                                                                          component, componentName, nameLastPart, componentPath,
                                                                          nodeId,
                                                                          null, null);
                                    var edgeStructure = new KLgraph.KLedgeStructure(rootNode, nodePart, 0);

                                    listVertices.Add(nodePart);
                                    listEdges.Add(edgeStructure);
                                    graph.AddVertex(nodePart);
                                    graph.AddEdge(edgeStructure);

                                    // Per ogni parte vengono aggiornate le entità ripetute.

                                    bool newIsLeaf = component.IGetChildrenCount() == 0;
                                    var  newRelativeTransformMatrix = GetTransformMatrix(component, swApplication);

                                    var newMyComponent = LC_AssemblyTraverse.ComputeNewRepeatedComponent(swApplication,
                                                                                                         component, nodeId, newRelativeTransformMatrix, i, newIsLeaf);

                                    //if (!newMyComponent.RepeatedEntity.listOfFaces.Any())
                                    //{
                                    //    if (nodePart.NodeShapeIsSphere(swApplication))
                                    //    {
                                    //        newMyComponent.IsSphere = true;
                                    //    }
                                    //    else
                                    //    {
                                    //        newMyComponent.IsSphere = false;
                                    //    }
                                    //}

                                    string namePath = component.Name2;
                                    //Console.WriteLine("Nome completo " + namePath);

                                    string nameFileComponent = namePath.Split('/').Last();
                                    //Console.WriteLine("Nome splittato " + nameFileComponent);

                                    //to get the last name after the last "\"
                                    namePath          = namePath.TrimEnd('-');
                                    nameFileComponent = nameFileComponent.Remove((nameFileComponent.LastIndexOf('-') + 1));
                                    //Console.WriteLine("Analizzo componente " + nameFileComponent);
                                    var indexOfFind =
                                        listOfMyListOfInstances.FindIndex(list => list.Name == nameFileComponent);
                                    if (indexOfFind != -1)
                                    {
                                        //Console.WriteLine("Trovata componente con lo stesso nome " + nameFileComponent);
                                        //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, "Istances.txt");
                                    }

                                    else
                                    {
                                        //    //Check of the component satisfy other shape criteria (volume and percentage of type of surfaces)
                                        //    var addCompForShape = false;

                                        //    foreach (MyListOfInstances instance in listOfMyListOfInstances)
                                        //    {
                                        //        var comparisonComponent = instance.ListOfMyComponent.First().Component;
                                        //        string comparisonComponentPath = comparisonComponent.Name2;
                                        //        //string nameFileComparisonComponent = comparisonComponentPath.Split('\\').Last();
                                        //        string nameFileComparisonComponent = comparisonComponentPath.TrimEnd('-');
                                        //        nameFileComparisonComponent =
                                        //            nameFileComparisonComponent.Remove(
                                        //                (nameFileComparisonComponent.LastIndexOf('-') + 1));

                                        //        var comparisonNode =
                                        //            (KLgraph.KLnodePart)
                                        //            listVertices.Find(
                                        //                part => part.NameLastPart == nameFileComparisonComponent);
                                        //        if (comparisonNode != null)
                                        //        {
                                        //            var comparisonShape = comparisonNode.KLshape;
                                        //            var comparisonStatistic = comparisonNode.KLstatistic;

                                        //            if (KLcriteriaCheck.Size.Volume(comparisonShape, shape, swApplication))
                                        //            {
                                        //                if (KLcriteriaCheck.Size.PercentageSurfacesType
                                        //                (comparisonStatistic,
                                        //                    statistic,
                                        //                    swApplication))
                                        //                {
                                        //                    namePath = comparisonComponent.Name2;
                                        //                    nameFileComponent = namePath.TrimEnd('-');
                                        //                    nameFileComponent =
                                        //                        nameFileComponent.Remove(
                                        //                            (nameFileComponent.LastIndexOf('-') + 1));

                                        //                    //nameFileComponent = namePath.Split('\\').Last();
                                        //                    //nameFileComponent = namePath;
                                        //                    indexOfFind =
                                        //                        listOfMyListOfInstances.FindIndex(
                                        //                            list => list.Name == nameFileComponent);
                                        //                    listOfMyListOfInstances[indexOfFind].ListOfMyComponent.Add(
                                        //                        newMyComponent);
                                        //                    //KLdebug.Print("       AGGIORNATA LISTA ESISTENTE: " + newMyComponent.Name + " con id" + newMyComponent.RepeatedEntity.idRE, "Istances.txt");
                                        //                    addCompForShape = true;
                                        //                    break;
                                        //                }
                                        //            }
                                        //        }
                                        //        else
                                        //        {
                                        //            swApplication.SendMsgToUser("Nodo ripetuto non recuperato");
                                        //        }
                                        //    }

                                        //    //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, "Istances.txt");
                                        //    }
                                    }

                                    i++;
                                }
                                else
                                {
                                    var shape        = KL_GetShapeAssembly(component, swApplication);
                                    var statistic    = KL_GetStatisticAssembly(component, swApplication);
                                    var nodeAssembly = new KLgraph.KLnodeAssembly(hashCode, transforation, fatherName,
                                                                                  fatherId,
                                                                                  component, componentName, nameLastPart, componentPath, nodeId,
                                                                                  childrenNumber, statistic, shape);
                                    var edgeStructure = new KLgraph.KLedgeStructure(rootNode, nodeAssembly, 0);

                                    listVertices.Add(nodeAssembly);
                                    listEdges.Add(edgeStructure);
                                    graph.AddVertex(nodeAssembly);
                                    graph.AddEdge(edgeStructure);

                                    graph = KL_GetGraphOfAssemblyComponents(component, nodeAssembly, ref listVertices,
                                                                            listEdges,
                                                                            ref listOfMyListOfInstances, swApplication);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        Console.WriteLine(e.Source);
                        throw;
                    }
                }
                else
                {
                    Console.WriteLine("Padre nullo");
                }
            }
            //Console.WriteLine("Ritorno il grafo");
            return(graph);
        }