Sorry for the weak post. I submitted this prematurely and didn't notice (subcosciously pressing ctrl+s is my worst good habit). There doesn't seem to be any way of editing the top post.
I've tried casting the SketchSegment as an Entitiy and get type errors on assignment. It seems weird that AddRelation takes an array of entities as an argument when there isn't an obvious way of retrieving entities from SketchSegments, Vertices, Etc. (all of the things that one would expect to add relations to). Please tell me I'm missing something simple.
I also seem to see language in the API documentation that leads me to believe that SketchSegments are Entities. If that's the case, why the type mismatch error?
I'm using Solidworks 2012 and writing the macro in VBA.
Instead of converting the SketchSegments into Entities, create an array of sketchSegments and pass it to the function. It works just fine.
Just check this sample code: -
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", -1.68043096009569E-02, 0.121369081820774, 0.101066255943181, False, 0, Nothing, 0)
Dim skSegment1 As Object
Dim skSegment2 As Object
Set skSegment1 = Part.SketchManager.CreateLine(-0.060294, -0.006474, 0#, -0.030619, 0.038038, 0#)
Set skSegment2 = Part.SketchManager.CreateLine(-0.030619, 0.038038, 0#, 0.025224, 0.032103, 0#)
Dim oSketch As SldWorks.Sketch
Set oSketch = Part.GetActiveSketch2()
Dim obj(1) As Object
Set obj(0) = skSegment1
Set obj(1) = skSegment2
Dim v As Variant
v = obj
Call oSketch.RelationManager.AddRelation(v, swConstraintType_PERPENDICULAR)
Hope it helps!!!
This may help a little, look up safe arrays in the API documentation:
Passing array data to SolidWorks in Visual Basic requires the use of a
VARIANT variable to hold the data. Put the data into an array and assign a VARIANT to the array. The VARIANT is
passed to an API function enclosed within parentheses.
Dim varArray As Variant
Dim DataArray(9) As double
‘ Assign values
DataArray(0) = 0.1
‘ Pass the data to SolidWorks
varArray = DataArray
Result = Object.Method( (varArray) ) ‘ The extra parentheses are required
Thanks for that, I was not aware of this (requirement?).
I did some experimenting and observed the following behavior (using Rajat's example above as reference):
1. If I Dim obj(1) as Variant, and use it in the same way as in Rajat's example, it throws a server exception when I call
2. If I Dim obj(1) as Object and assign it to a Variant v, I can pass it to
without any hickups (this is literally Rajat's example).
3. If I Dim obj(1) as Object and pass it to
It also works (omitting the assignment to a Variantv).
Thank you, this is working for me.