2 Replies Latest reply on Jan 20, 2015 2:25 AM by Ijsbrand Schipperus

    How to find the face of a cosmetic thread programmatically?

    Ijsbrand Schipperus

      I have a bit of code that cycles through all features and subfeatures, then finds a cosmetic thread feature. Now I want to color the face of the cosmetic thread (the same face that shows the shaded cosmetic thread), but I can't figure out how.

       

      Right now I think I should get the Edge from ICosmeticThreadFeatureData to select an adjacent face, but it doesn't return an Edge...

       

      Does anyone have the solution for me??

       

      Last part of Code (C#, but if you have the solution in VB it's fine for me too):

       

      // At the start I have a Feature swSubFeat, this is a cosmetic thread (otherwise the code skips this part)

       

      ICosmeticThreadFeatureData FData;

      FData = swSubFeat.IGetDefinition();

      Edge FEdge;

      FEdge = FData.Edge;

       

      I can provide the full code as well.

       

        • Re: How to find the face of a cosmetic thread programmatically?
          Keith Rice

          Hi,

           

          You need to use ICosmeticThreadFeatureData::AccessSelections in order to get the edge selection. The following code will select the circular face on which the cosmetic thread is applied. To run it, open a new C# macro and paste in the code. Select the cosmetic thread in the feature manager tree before running.

           

          using SolidWorks.Interop.sldworks;
          using SolidWorks.Interop.swconst;
          using System.Runtime.InteropServices;
          
          namespace Macro1.csproj
          {
              public partial class SolidWorksMacro
              {
                  public void Main()
                  {
                      ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;
                      SelectionMgr swSelMgr = (SelectionMgr)swModel.SelectionManager;
                      Feature swFeat = (Feature)swSelMgr.GetSelectedObject6(1, -1);
                      CosmeticThreadFeatureData swData = (CosmeticThreadFeatureData)swFeat.GetDefinition();
                      swData.AccessSelections(swModel, null);
                      Edge swEdge = swData.Edge;
                      if (swEdge == null)
                          swApp.SendMsgToUser("nothing");
                      else
                      {
                          object[] adjacentfaces = (object[])swEdge.GetTwoAdjacentFaces2();
                          Entity swEnt = null;
                          Face2 swFace1 = (Face2)adjacentfaces[0];
                          Face2 swFace2 = (Face2)adjacentfaces[1];
                          Surface swSurf = (Surface)swFace1.GetSurface();
                          if (swSurf.IsCylinder())
                              swEnt = (Entity)swFace1;
                          else
                              swEnt = (Entity)swFace2;
          
                          swEnt.Select(false);
                      }
          
                      //swData.ReleaseSelectionAccess();
                  }
          
                  public SldWorks swApp;
              }
          }
          

           

          Normally you would use the ReleaseSelectionAccess() method of a particular feature data interface to release the selection access, but AccessSelections in this case doesn't put the feature back in a rollback state, so it doesn't appear to be necessary.

           

          Keith

          SolidWorks API Automation