7 Replies Latest reply on Jan 30, 2018 5:24 AM by Gabriele Sorrento

    C#: Mesh Export

    Gabriele Sorrento

      Hi guys,

      do you know a way to iterate over all objects of a document and get their mesh description (nodes, facets, hopefully color & transparency)?

      Thanks!!

        • Re: C#: Mesh Export
          Christian Chu

          Luke has created some C# tutorials and posted them on youtube

          I think you'd better start from beginning than jumping in and programming.

          • Re: C#: Mesh Export
            Gabriele Sorrento

            Sorry, I have been watching his videos and I also read his blog but I cannot find exactly what I am looking for.

            I am not asking to post a solution but even a hint would be helpful.

            • Re: C#: Mesh Export
              Gabriele Sorrento

              Thanks for your support. This is the solution I could find to get the meshes iterating over the assembly:

               

              ...
              if (nDocumentType == swDocumentTypes_e.swDocASSEMBLY)
                              {
                                  IAssemblyDoc swAssembly = null;
                                   swAssembly = (IAssemblyDoc)swModel;
                                  int n_comp = swAssembly.GetComponentCount(false);
                                  object[] comps = swAssembly.GetComponents(false);
              
                                  foreach (Component2 cp in comps)
                                  {
                                      List<float> triangles = new List<float>();
                                      List<float> normals = new List<float>();
                                      Object[] aBodies;
                                      aBodies = (Object[])(cp.GetBodies2((int)swBodyType_e.swSolidBody));
                                      swModel.ClearSelection2(true);
                                      foreach (IBody2 swBody in aBodies)
                                      {
                                          bool bSelected = swModelExtension.SelectByID2(swBody.Name, "SOLIDBODY", 0.0, 0.0, 0.0, true, 0, null, (int)swSelectOption_e.swSelectOptionDefault);
                                          IFace2 swFace = null;
                                        
                                          swFace = swBody.IGetFirstFace();
                                          while (swFace != null)
                                          {
                                              int iNumEdges = swFace.GetEdgeCount();
                                              IEdge ff = swFace.GetEdges()[0];
                                              
                                              // TOPOLOGICALLY DISCONNECTED TRIS    <----------------------------------------
                                              int numtris = swFace.GetTessTriangleCount();
                                              float[] norms = (float[])swFace.GetTessNorms();
                                              float[] tris = (float[])swFace.GetTessTriangles(true);
                                              for (int t = 0; t < numtris ; t++)
                                              {
                                                  triangles.Add(tris [i]);
                                                   normals.Add(norms [i]);
                                              }
                                               swFace = swFace.IGetNextFace();
                                          }
                                      }
              ...
              

               

              now I have two issues:

              - the triangles are not topologically ordered (each triangle contains the coordinates of the three points rather than IDs of a node list)

              - the parts contained in the assembly seem to shifted on the origin of the assembly refsystem. I guess they contain the information of the related xform in the .Transform method, but when I apply it to the single body the tessellation coordinates seem not to be affected. Anyone knows a way to apply the transformation to the tessellation in order to place the components in the right way?

                • Re: C#: Mesh Export
                  Jacob Corder

                  if you want the coordinates in the model which is exempt from ALL possible assembly transformations then you simply do this

                   

                  Dim SwModel as modeldoc2 = CP.GetModelDoc2

                  Dim PDoc as partdoc = swmodel

                  Dim SwBodies as object = Pdoc.GetBodies2

                  loop on the bodies here

                   

                   

                   

                  these tesselation points will be MODEL Based, NOT ASSEMBLY BASED.

                   

                   

                  if you need to do math transformations i would not use solidworks math class. i would use my own or

                  System.Windows.Media.Media3D Namespace

                  I would build an infrastructure of classes to accomodate all of this math.


                  Create TriangleClass, contains references to 3 points in the TesselationCollectionClass

                  Create a TesselationCollectionClass contains all triangles and points

                  triangle contains point0, point1, point2,and access to  TesselationCollectionClass

                  Point classes contains x,y,z in some form

                   

                  TesselationCollection contains All Points in a list

                  TesselationCollection contains all triangles

                  TesselationCollection allows for Transformation Multiplication

                  in the tesselationCollection class you have a method MultiplyTransform(some transformation arguments)

                   

                  execute the multiplication across all points.

                   

                  all triangles will automatically be recalculated because they point to the  Point3D class

                   

                  basically you are making a huge mathutility

                  the multimedia3D namespace contains all of the transformation math so you dont need to deal with it. its pretty easy to use with the transformations that solidworks gives you

                  The beauty of this is you will definately want to use Parallelism.

                  TASK::PARALLEL::FOREACH.

                  it could even be calculated using the graphics card if your tesselation point count gets to be too high. read up on CUDA Code if you haven't already

                  it will calculate it at a very fast pace.

                  and its by far the easiest thing to do. 

                  • Re: C#: Mesh Export
                    Simon Turner

                    To answer your first question, an alternative way to tessellate an object is to use the ITessellation interface.

                    There is an example in the help - Tessellate a Body (C#)

                  • Re: C#: Mesh Export
                    Gabriele Sorrento

                    Thank you all for your replies