7 Replies Latest reply on Mar 12, 2018 7:29 PM by Jacob Corder

    ModelDoc2::SketchAddConstraints or ISketchRelationManager::AddRelation?

    John Alexander

      API Friends,

       

      2012 SOLIDWORKS API Help - AddRelation Method (ISketchRelationManager)

       

      2012 SOLIDWORKS API Help - SketchAddConstraints Method (IModelDoc2)

       

      The difference between these two methods seems to be that one requires an array of Entities as an argument and the other requires the entities to be pre-selected. Both require the constraint type to be specified. Otherwise, they should both have the effect of adding sketch relations/constraints to a set of sketch entities.

       

      I dislike selection-driven API functions but recognize that they are inevitable in some instances. As such, I would prefer to use AddRelation. However, I have not been able to get AddRelation to work and I am wondering if anyone who has experience with programmatically adding sketch relations can shed some light on whether or not this is a dead end or I'm just using it incorrectly. I haven't been able to find any examples of its usage.

       

      The documentation says that it takes an array of Entities as an argument. For my application, the goal is to add a constraint between two SketchPoints and the StartPoint/Endpoint of a SketchLine.

       

      This is a snippet of my attempt, the CSPPointGraphEdge type is just a container that I am using for a broader purpose. This function is part of a class so any variables prefixed by "p" have been initialized even though they aren't in the example.

       

      Private Type CSPPointGraphEdge
          swSketchLine    As SldWorks.SketchLine
          swSketchPoint1  As SldWorks.SketchPoint
          swSketchPoint2  As SldWorks.SketchPoint
          swMathPoint1    As SldWorks.MathPoint
          swMathPoint2    As SldWorks.MathPoint
          swDirectionVect As SldWorks.MathVector
      End Type
      
      Private Function drawLineForCSPPointGraphEdge(PointGraphEdge As CSPPointGraphEdge)
          Dim swSketchManager         As SldWorks.SketchManager
          Dim swSketchRelationManager As SldWorks.SketchRelationManager
          Dim swSketchRelation        As SldWorks.SketchRelation
          Dim swFeature               As SldWorks.Feature
          Dim x1 As Double
          Dim y1 As Double
          Dim z1 As Double
          Dim x2 As Double
          Dim y2 As Double
          Dim z2 As Double
      
              '<setup to start editing the sketch>
          Set swSketchManager = pModel.SketchManager
          Set swSketchRelationManager = pSketch.RelationManager
          Set swFeature = pSketch
          If pModel.Extension.SelectByID2(swFeature.Name, "SKETCH", 0, 0, 0, False, -1, Nothing, swSelectOption_e.swSelectOptionDefault) Then
              Debug.Assert True
          Else
              Debug.Assert False
          End If
          swSketchManager.InsertSketch True
          swSketchManager.AddToDB = True
          '</setup to start editing the sketch>
          
          'PointGraphEdge.swMathPoint2
          x1 = PointGraphEdge.swMathPoint1.ArrayData(0)
          y1 = PointGraphEdge.swMathPoint1.ArrayData(1)
          z1 = PointGraphEdge.swMathPoint1.ArrayData(2)
          x2 = PointGraphEdge.swMathPoint2.ArrayData(0)
          y2 = PointGraphEdge.swMathPoint2.ArrayData(1)
          z2 = PointGraphEdge.swMathPoint2.ArrayData(2)
          
          Set PointGraphEdge.swSketchLine = swSketchManager.CreateLine(x1, y1, z1, x2, y2, z2)
          
           'I want to add the relation here using SketchRelationManager::AddRelation
          Dim vEntities       As Variant
          Dim swSketchPoint   As SldWorks.SketchPoint
          Set swSketchPoint = PointGraphEdge.swSketchLine.GetStartPoint2
          ReDim vEntities(1)
          Set vEntities(0) = swSketchPoint
          Set vEntities(1) = PointGraphEdge.swSketchPoint1
          'The following line throws an automation error
          Set swSketchRelation = swSketchRelationManager.AddRelation(vEntities, swConstraintType_e.swConstraintType_COINCIDENT)
          
          '<setup to stop editing the sketch>
          swSketchManager.AddToDB = False
          swSketchManager.InsertSketch True
          '</setup to stop editing the sketch>
          
      End Function
      

       

      • I can't seem to cast the SketchPoints as Entities. This problem seems vaguely familiar to me but I can't remember why it won't work. The documentation says taht the function takes entities as an argument and I know from experience that sketch points can have sketch relations assigned to them, naturally that would imply that SketchPoints are (or inherit from?) Entities.

       

      • When I just build a variant array and pass it to AddRelation, the macro throws with a run-time "automation error". This isn't surprising as I already failed to cast those SketchPoints as Entities.

       

      Any thoughts or comments would be much appreciated.

       

      Thank you,

       

      John Alexander

        • Re: ModelDoc2::SketchAddConstraints or ISketchRelationManager::AddRelation?
          Jacob Corder

          Change VEntities from variant to Object()


          Dim VEntities(1) as object

           

          the documention says array of entities alot.  Some times its a DispatchWrapper array, or just an object array

            • Re: ModelDoc2::SketchAddConstraints or ISketchRelationManager::AddRelation?
              John Alexander

              Thanks, it looks like this was the problem.

               

                  Dim swObjects() As Object

                  ReDim swObjects(1)

                 

                  Set swObjects(0) = PointGraphEdge.swSketchLine.GetStartPoint2

                  Set swObjects(1) = PointGraphEdge.swSketchPoint1

                 

                  Set swSketchRelation = swSketchRelationManager.AddRelation(swObjects, swConstraintType_e.swConstraintType_COINCIDENT)

                 

                  'modeldoc::SketchAddConstraints method

                  'Debug.Assert PointGraphEdge.swSketchLine.GetStartPoint2.Select4(False, Nothing)

                  'Debug.Assert PointGraphEdge.swSketchPoint1.Select4(True, Nothing)

                  'pModel.SketchAddConstraints "sgCOINCIDENT"

              It successfully adds the coincident constraint between the sketch line start point and a sketchpoint.

               

              Looks like I need to read up on the differences between Variants and Objects in VBA. I basically never use the type "Object".

                • Re: ModelDoc2::SketchAddConstraints or ISketchRelationManager::AddRelation?
                  Brendan Collins

                  I ran into this in C# and absolutely HAD TO pass `DispatchWrapper[]` (passing `Object[]` WILL  throw...)

                  For whoever it may help, here is an example helper method that performs the conversion to `DispatchWrapper[]`.

                  Credit to Michael Kozikowski at API Support for providing clarity on this also.

                   

                                 using System.Runtime.InteropServices;

                                 ...

                                 public DispatchWrapper[] GetDispatchWrapperArray(object[] swObjects)

                                 {

                                      var dispWraps = new DispatchWrapper[swObjects.Length];

                                      for (int i = 0; i < swObjects.Length; i++)

                                           dispWraps[i] = new DispatchWrapper(swObjects[i]);

                   

                                      return dispWraps;

                                 }

                   

                  Example Usage

                   

                            // Get the selected objects for passing to AddRelation

                            object[] selEnts = ... // Get the objects via API (e.g. SelectionManager.GetSelectedObject6)

                   

                            // need to pass a special DispatcherWrapper array for AddRelation to work

                            DispatchWrapper[] dispArr =  GetDispatchWrapperArray(selEnts);

                   

                             // add relation

                             SketchRelation skRel = swSketch.RelationManager.AddRelation(dispArr, (int)swConstraintType_e.swConstraintType_COINCIDENT); 

              • Re: ModelDoc2::SketchAddConstraints or ISketchRelationManager::AddRelation?
                Amen Allah Jlili

                It seems that the array of entities need to drawn from the SelectionManager. Apparently, the return of GetSelectedObject has some interrior magic that bare API objects don't have. When you think of it, you do literally have to to select the entities before you add a relation manually (with your mouse). Somehow, that seem to be the only reason to explain this.

                 

                Take a look at this:

                #CODE|Add Sketch Relations

                 


                Cheers,
                Amen
                CADHero.com