Creating a 3D geometric conincident constraint in a 3D Sketch via VBA (testing)

Creating a 3D geometric conincident constraint in a 3D Sketch via VBA (testing)

Anonymous
Not applicable
606 Views
3 Replies
Message 1 of 4

Creating a 3D geometric conincident constraint in a 3D Sketch via VBA (testing)

Anonymous
Not applicable

Hi,

 

Created a 3D sketch and would like to constrain the end point of the line to another point which is constrained to 2D sketch.  We are able to constrain the 3D sketch to the point via the UI, but when attempting to do via code (VBA for testing) we are getting an error.

 

To be specific, a Run-time error '13': type mismatch.

   Dim oCoinConst As Inventor.CoincidentConstraint3D
   Set oCoinConst = oSketch3D.GeometricConstraints3D.AddCoincident(oLineA, OriginPoint)

 

oLineA is a Sketch3D entity

OriginPoint is a Point

 

Annotation 2020-08-26 112626.png

 

Module code:

Public Sub Create3DSketch(name As String, mp As ObjectCollection) '  mp() As WorkPoint)
  
  Dim oCompDef As PartComponentDefinition
  Set oCompDef = myPartDoc.ComponentDefinition
  
  'Dim oSketch3D As Sketch3D (global variable)
  Set oSketch3D = oCompDef.Sketches3D.Add
  
  ' Name the newly created 3d sketch
  oSketch3D.name = UCSSkName
    
  Dim oTG As TransientGeometry
  Set oTG = ThisApplication.TransientGeometry
  
  Dim oOriginSP As SketchPoint3D
  ''set oOriginSP =
  Set oOriginSP = oSketch3D.SketchPoints3D.Add(OriginPoint, False)
  ''Call oOriginSP.ConnectTo(OriginPoint)
  
  ''   https://forums.autodesk.com/t5/inventor-customization/constraining-a-sketch-point-to-the-origin-using-vba/td-p/2387173
    
  Dim oLineA As SketchLine3D
  Dim oLineXaxis As SketchLine3D
  Dim oLineYaxis As SketchLine3D
  
  Set oLineA = oSketch3D.SketchLines3D.AddByTwoPoints(oOriginSP, mp.Item(2))
  Set oLineXaxis = oSketch3D.SketchLines3D.AddByTwoPoints(oLineA.EndSketchPoint, mp.Item(3), False)
  Set oLineYaxis = oSketch3D.SketchLines3D.AddByTwoPoints(oLineA.EndSketchPoint, mp.Item(4), False)
 
  ' Constrain oLineA to the z-axis
  Dim oParallalZ As ParallelToZAxisConstraint3D
  Set oParallalZ = oSketch3D.GeometricConstraints3D.AddParallelToZAxis(oLineA)
    
  ' Constrain oLineXaxis to the x-axis
  Dim oParallelX As ParallelToXAxisConstraint3D
  Set oParallelX = oSketch3D.GeometricConstraints3D.AddParallelToXAxis(oLineXaxis)
  
  ' Constrain oLineYaxis to the y-axis
  Dim oParallelY As ParallelToYAxisConstraint3D
  Set oParallelY = oSketch3D.GeometricConstraints3D.AddParallelToYAxis(oLineYaxis)
    
  oLineA.Construction = True
  oLineXaxis.Construction = True
  oLineYaxis.Construction = True
  
  Dim oLineLenConstr3D As LineLengthDimConstraint3D
  Set oLineLenConstr3D = oSketch3D.DimensionConstraints3D.AddLineLength(oLineA)
  ' oLineLenConstr3D.Driven = False
  
'  Dim oModelParam As UserParameter
'  Set oModelParam = oLineLenConstr3D.Parameter
      
   ' Constrain mp(0) with OriginPoint
   Dim oCoinConst As Inventor.CoincidentConstraint3D
   Set oCoinConst = oSketch3D.GeometricConstraints3D.AddCoincident(oLineA, OriginPoint)
         

End Sub

 

Will eventually tie the 3D sketch z elevation to a user parameter, but first things first.  

 

Thanks for reading.

 

 

 

0 Likes
607 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable

Doing some digging in the API help document and come to find out that trying to place the a conincident constraint between two points will fail, and it states that we should use the ConnectTo method on the sketchpoint3d object to merge two points.

 

 

   'SketchPoint3D.ConnectTo( ConstrainingPoint As Object )
   Call oLineA.StartPoint.ConnectTo(OriginPoint)

 

Trying that approach, but now we're getting an error dialog.

Annotation 2020-08-26 132644.png

 

Keep, keeping on.

 

I'll figure it out eventually. 🙂

 

M.

0 Likes
Message 3 of 4

JelteDeJong
Mentor
Mentor

this works for me. (it assumes that there are 2x 2d sketches with at least 1 line in them)

Dim doc As PartDocument = ThisDoc.Document
Dim def = doc.ComponentDefinition

Dim sketch1 As Sketch = def.Sketches.Item(1)
Dim sketch2 As Sketch = def.Sketches.Item(2)
Dim line1 = sketch1.SketchLines.Item(1)
Dim line2 = sketch2.SketchLines.Item(1)

Dim sketch3d As Sketch3D = def.Sketches3D.Add()


Dim p1 = line1.StartSketchPoint
Dim p2 = line2.StartSketchPoint
sketch3d.SketchLines3D.AddByTwoPoints(p1, p2)

' or the log way

Dim p3 = ThisApplication.TransientGeometry.CreatePoint(1, 1, 1)
Dim p4 = ThisApplication.TransientGeometry.CreatePoint(2, 2, 2)
Dim line As SketchLine3D = sketch3d.SketchLines3D.AddByTwoPoints(p3, p4)

line.StartSketchPoint.ConnectTo(line1.EndSketchPoint)
line.EndSketchPoint.ConnectTo(line2.EndSketchPoint)

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 4 of 4

Anonymous
Not applicable

No joy.

 

Works via iLogic, but not via VBA. 😞

 

Annotation 2020-08-26 152837.png

 

  
  Set oLineA = oSketch3D.SketchLines3D.AddByTwoPoints(oOriginSP, mp.Item(2))
  Set oLineXaxis = oSketch3D.SketchLines3D.AddByTwoPoints(oLineA.EndSketchPoint, mp.Item(3), False)
  Set oLineYaxis = oSketch3D.SketchLines3D.AddByTwoPoints(oLineA.EndSketchPoint, mp.Item(4), False)
  
  'SketchPoint3D.ConnectTo( ConstrainingPoint As Object )
  Call oLineA.StartSketchPoint.ConnectTo(OriginPoint)

 

Getting the error on the last line above.

0 Likes