AnsweredAssumed Answered

ModelDoc2::SketchAddConstraints or ISketchRelationManager::AddRelation?

Question asked by John Alexander on Sep 5, 2017
Latest reply on Apr 12, 2019 by Jorn Bjarning

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
        Debug.Assert False
    End If
    swSketchManager.InsertSketch True
    swSketchManager.AddToDB = True
    '</setup to start editing the sketch>
    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