How to create a UCS with using Work Point with vb.net

How to create a UCS with using Work Point with vb.net

niranjan934
Contributor Contributor
1,452 Views
7 Replies
Message 1 of 8

How to create a UCS with using Work Point with vb.net

niranjan934
Contributor
Contributor

I tried several times but I can't create a ucs with work point using vb.net. If anybody know please post the code and also is possible to use design assistant with vb.net? if yes,could you please post the code design assistant also.

 

0 Likes
Accepted solutions (1)
1,453 Views
7 Replies
Replies (7)
Message 2 of 8

JelteDeJong
Mentor
Mentor
Accepted solution

i have run this code in vb.net/visualstudio and as ilogic rule. it works for me. does thsi help you?

Dim oTg = ThisApplication.TransientGeometry
Dim doc As PartDocument = ThisApplication.ActiveDocument

' create 3 random workpoints for the example

Dim wp1 As WorkPoint = doc.ComponentDefinition.WorkPoints.AddFixed(oTg.CreatePoint(1, 2, 3))
Dim wp2 As WorkPoint = doc.ComponentDefinition.WorkPoints.AddFixed(oTg.CreatePoint(4, 5, 6))
Dim wp3 As WorkPoint = doc.ComponentDefinition.WorkPoints.AddFixed(oTg.CreatePoint(4, 5, 9))


' create the UCS
Dim ucsDef As UserCoordinateSystemDefinition = doc.ComponentDefinition.UserCoordinateSystems.CreateDefinition()
ucsDef.SetByThreePoints(wp1, wp2, wp3)
doc.ComponentDefinition.UserCoordinateSystems.Add(ucsDef)

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 3 of 8

Anonymous
Not applicable

@JelteDeJong I am also trying to create a UCS. I have tried adapting what you have in your solution but it isn't working for me. I have an array of WorkPoints called points() and I want to create a UCS based on points(0), points(1), and points(2). Do you know how I can accomplish this? For reference I am using the Inventor API.

0 Likes
Message 4 of 8

Ralf_Krieg
Advisor
Advisor

Hello

 

If there are three workpoints in your  array, it should work. Can you post the code how you get the three points?

Are you trying to create UCS in part or assembly?


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 5 of 8

MICHAEL.JONES.AMCE
Advocate
Advocate

I've been working on a VB.net Addin to place a UCS using a 3D Sketch.

 

Here's some of the code which creates the 3D sketch and then creates the UCS.

        ' Create the 3D sketch which will be utilized to place the UCS
        Public Sub Create3DSketch(name As String, mp() As Point, deltaZ As Double)

            Dim XOffset As Double = Math.Round(12.0# * 2.5397, 4)
            Dim YOffset As Double = Math.Round(12.0# * 2.5397, 4)

            Dim oCompDef As PartComponentDefinition = g_MyPartDoc.ComponentDefinition

            Dim RotateAngleDeg As Double
            RotateAngleDeg = Val(g_UCSForm.lbAngleInput.SelectedItem.ToString)

            If g_DebugOn Then
                MsgBox("Angle selected : " & CStr(RotateAngleDeg) & vbCrLf & "Delta Z :" & CStr(deltaZ))
            End If

            Dim RotateAngleRadian As Double = ConvertAngleToRad(RotateAngleDeg)

            'Dim oSketch3D As Sketch3D
            My3DSketch = oCompDef.Sketches3D.Add

            Dim oTG As TransientGeometry = g_InventorApplication.TransientGeometry

            Dim oLineA As SketchLine3D
            Dim oLineB As SketchLine3D
            Dim oLineC As SketchLine3D

            '' 10/29/30 fix  for zero deltaZ
            If deltaZ = 0 Then
                deltaZ = 0.001 ' centimeters, so 1 micron
            End If

            oLineA = My3DSketch.SketchLines3D.AddByTwoPoints(oTG.CreatePoint(MyPoint.Geometry.X, MyPoint.Geometry.Y, 0#),
                                                             oTG.CreatePoint(MyPoint.Geometry.X, MyPoint.Geometry.Y, deltaZ), False)
            oLineA.Construction = True

            Dim TempPointB As Point
            TempPointB = oTG.CreatePoint((Math.Cos(RotateAngleRadian) * XOffset) + MyPoint.Geometry.X,
                                    (Math.Sin(RotateAngleRadian) * XOffset) + MyPoint.Geometry.Y, deltaZ)
            oLineB = My3DSketch.SketchLines3D.AddByTwoPoints(oLineA.EndSketchPoint, TempPointB, False)
            oLineB.Construction = True

            Dim TempPointC As Point
            ' Set TempPointC = oTG.CreatePoint(myPoint.Geometry.X, myPoint.Geometry.Y + YOffset, deltaZ)
            TempPointC = oTG.CreatePoint((-Math.Sin(RotateAngleRadian) * YOffset) + MyPoint.Geometry.X,
                                    (Math.Cos(RotateAngleRadian) * YOffset) + MyPoint.Geometry.Y, deltaZ)
            oLineC = My3DSketch.SketchLines3D.AddByTwoPoints(oLineA.EndSketchPoint, TempPointC, False)
            oLineC.Construction = True

            ' https://adndevblog.typepad.com/manufacturing/2013/07/inventor-api-create-constraints-for-3d-sketch.html
            ' Constrain oLineA to the z axis

            Dim oParallelConstr3DZ As ParallelToZAxisConstraint3D
            'Dim oParallelConstr3DX As ParallelToXAxisConstraint3D
            'Dim oParallelCOnstr3DY As ParallelToYAxisConstraint3D

            oParallelConstr3DZ = My3DSketch.GeometricConstraints3D.AddParallelToZAxis(oLineA)
            'Set oParallelConstr3DX = My3DSketch.GeometricConstraints3D.AddParallelToXAxis(oLineB)
            'Set oParallelCOnstr3DY = My3DSketch.GeometricConstraints3D.AddParallelToYAxis(oLineC)

            '' 10/21/20 ADDING USER PARAMETER IS NOT FUNCTIONING..
            Dim oUserParams As UserParameters
            oUserParams = g_MyPartDoc.ComponentDefinition.Parameters.UserParameters
            Dim oUserParam As Parameter
            ' Set oUserParam = oUserParams.AddByValue(UCSName & UserParamNameSuffix, deltaZ, kDefaultDisplayLengthUnits)

            ' Constrain oLineA Length to zed offset
            Dim oLineLenConstr3DZed As LineLengthDimConstraint3D
            oLineLenConstr3DZed = My3DSketch.DimensionConstraints3D.AddLineLength(oLineA)
            oLineLenConstr3DZed.Driven = False  ' set to true to use an user parameter

            Dim oModelparam As ModelParameter
            oModelparam = oLineLenConstr3DZed.Parameter
            oModelparam.Value = Math.Abs(deltaZ)

            ' Constrain oLineB length
            Dim oLineLenconstr3d As LineLengthDimConstraint3D
            oLineLenconstr3d = My3DSketch.DimensionConstraints3D.AddLineLength(oLineB)

            ' Set the driven property to false to ensure the parameter is a model parameter
            oLineLenconstr3d.Driven = False
            oUserParam = oLineLenconstr3d.Parameter

            ' set the new value in base units (cm)
            oUserParam.Value = XOffset

            ' Constrain oLineC length
            oLineLenconstr3d = My3DSketch.DimensionConstraints3D.AddLineLength(oLineC)

        End Sub

        ' Routine to create the UCS Definition from the 3D sketch
        Private Sub CreateNewUCSDef(ou As UserCoordinateSystemDefinition, mSK As Sketch3D)
            Dim skLineA As SketchLine3D
            Dim skLineB As SketchLine3D
            Dim skLineC As SketchLine3D

            skLineA = mSK.SketchLines3D.Item(1)
            skLineB = mSK.SketchLines3D.Item(2)
            skLineC = mSK.SketchLines3D.Item(3)

            ou.SetByThreePoints(skLineA.EndSketchPoint, skLineB.EndSketchPoint, skLineC.EndSketchPoint)

        End Sub

 

The overall Addin is still very much a work in progress, but if you want I can zip up the VB.net solution and attach to the forum.

0 Likes
Message 6 of 8

Anonymous
Not applicable

@Ralf_Krieg Here is my code. I am doing this in a part, not an assembly. About two thirds into my program is where I'm trying to create my coordinate system

Sub ExportWorkPointsV2()

'How to use: create your axes in this order: 1st sraight section, 1st bend, 2nd straight, 2nd bend... until final straight section.
'Create a workpoint for the start point and end point. Select all axes and the 2 points, run the macro. Macro will create points at
'each intersection of axis, place them in order, create a UCS using points 1-3, offset the points by the UCS, measure the distance
'of the bend axis to a straight axis, save that as a radius, export the XYZ and R of each point to an excel sheet.

Dim ready As Long
ready = 1
If ready = 0 Then
    MsgBox "This macro is not ready yet"
    Exit Sub
End If

    Dim partDoc As PartDocument
    Dim partCompDef As PartComponentDefinition
    
    If ThisApplication.ActiveDocumentType = kPartDocumentObject Then
        Set partDoc = ThisApplication.ActiveDocument
        Dim partDef As PartComponentDefinition
        Set partDef = partDoc.ComponentDefinition
        
    Else
        MsgBox "A part must be active"
        Exit Sub
    End If
    
    Dim points() As WorkPoint
    Dim inputPoints() As WorkPoint
    Dim axes() As WorkAxis
        
    Dim axisCount As Long
    axisCount = 0
    Dim inputPointCount As Long
    inputPointCount = 0
    
    'verifying the user selected points before beginning
    If partDoc.SelectSet.Count > 0 Then
    
        ReDim axes(partDoc.SelectSet.Count - 1)
        ReDim inputPoints(partDoc.SelectSet.Count - 1)
                        
        Dim selectedObj As Object
        
        'we are searching through each of the selected objects to look for work axes and work points and storingthem in arrays
        For Each selectedObj In partDoc.SelectSet
            
            'MsgBox "Type of object is: " & TypeName(selectedObj)
            
            
            If TypeOf selectedObj Is WorkAxis Then
                Set axes(axisCount) = selectedObj
                axisCount = axisCount + 1
                'MsgBox "Axis found, axisCount = " & Format(axisCount, "0")
            End If
            
            If TypeOf selectedObj Is WorkPoint Then
                'MsgBox "Type of object is: " & TypeName(selectedObj)
                 Set inputPoints(inputPointCount) = selectedObj
                 inputPointCount = inputPointCount + 1
                 'MsgBox "Input point found, inputPointCount = " & Format(inputPointCount, "0")
             End If
        Next
        ReDim Preserve axes(axisCount)
        ReDim Preserve inputPoints(inputPointCount)
        'MsgBox "Axis count is "
        
     '   ReDim Preserve axes(axisCount - 1)
    Else
        MsgBox "Nothing selected. Please select all of your bend and straight axes, your start point, and your end point, then try again"
        Exit Sub
    End If
    
    'verifying that the user only input 2 points, will exit the macro if they did more
    If UBound(inputPoints) <> 2 Then
        If UBound(inputPoints) > 2 Then
            MsgBox "More than 2 points selected. Are you sure you only preselected your start and end points?"
            Exit Sub
        Else
            MsgBox "Not enough points selected. Make sure you preselect your start and end points"
            Exit Sub
        End If
    End If
    
    'making sure the user preselects axes too
    If UBound(axes) < 3 Then
        MsgBox "You need to select at least 3 axes"
        Exit Sub
    End If
    
    MsgBox "Number of selected objects: " & Format(partDoc.SelectSet.Count, "0") & vbCrLf & "Number of axes: " & Format(UBound(axes), "0") & vbCrLf & "Number of points: " & Format(UBound(inputPoints), "0") & vbCrLf & "Number of unrecognized geometry selected: " & Format(partDoc.SelectSet.Count - UBound(axes) - UBound(inputPoints), "0")
    
   ' MsgBox "Number of axes: " & Format(UBound(axes), "0") & " number of points: " & Format(UBound(inputPoints), "0")
    
    'establishing an array to store radii values
    Dim radCount As Integer
    radCount = UBound(axes) - 1
    radCount = radCount / 2
        
    Dim rad() As Double
    ReDim Preserve rad(radCount)
 '  rad(0) = ThisApplication.MeasureTools.GetMinimumDistance(axes(0), axes(1)) / 2.54
 '  MsgBox "Radius of 1st bend is: " & Format(rad(0), "0.000") & " in and number of radii is: " & Format(UBound(rad), "0")
    
    'looping to find the radii values, and create points at axis intersections, then storing the data to their respective arrays
    ReDim Preserve points(partDoc.SelectSet.Count)
    
    Set points(0) = inputPoints(0)
    rad(0) = 0
    Dim j As Integer
    j = 0
    For i = 0 To radCount - 1
        'we do not need to add an i = i + 1 in order to iterate during a For Next loop
        'we do need to iterate j by 2 to iterate through the axes
        rad(i + 1) = ThisApplication.MeasureTools.GetMinimumDistance(axes(j), axes(j + 1))
        rad(i + 1) = rad(i + 1) / 2.54
        Set points(i + 1) = partDef.WorkPoints.AddByTwoLines(axes(j), axes(j + 2))
       'i = i + 1
        j = j + 2
    Next
    Set points(i) = inputPoints(1)
    ReDim Preserve points(UBound(points))
    
    rad(i) = 0
    For k = 0 To radCount - 1
        Dim newPoint As Point
        Set newPoint = points(k).Point
        'format(newpoint.X,"0.000")
        
        MsgBox "Point " & k + 1 & ": " & Format(newPoint.X, "0.000") & ", " & Format(newPoint.Y, "0.000") & ", " & Format(newPoint.Z, "0.000")
        
    
    Next

   'using points 1, 2 , 3 to create UCS
   Dim oTg As TransientGeometry
   Set oTg = ThisApplication.TransientGeometry

   Dim p1 As Point
   Set p1 = partDoc.ComponentDefinition.WorkPoints.AddFixed(oTg.CreatePoint(points(0).Point.X, points(0).Point.Y, points(0).Point.Z))
   Dim p2 As Point
   Set p2 = partDoc.ComponentDefinition.WorkPoints.AddFixed(oTg.CreatePoint(points(1).Point.X, points(1).Point.Y, points(1).Point.Z))
   Dim p3 As Point
   Set p3 = partDoc.ComponentDefinition.WorkPoints.AddFixed(oTg.CreatePoint(points(2).Point.X, points(2).Point.Y, points(2).Point.Z))



   Dim ucsDef As UserCoordinateSystemDefinition
   Set ucsDef = partDoc.ComponentDefinition.UserCoordinateSystems.CreateDefinition.SetByThreePoints(p1, p2, p3)
   partDoc.ComponentDefinition.UserCoordinateSystems.Add (ucsDef)

   'creating a matrix to offset the workpoints
   Dim ucsMat As Matrix
   Set ucsMat = ucsDef.Transformation
   Call ucsMat.Invert

   'Dim newUCS As UserCoordinateSystem
   'Set newUCS = partDef.UserCoordinateSystems.Add.CreateDefinition.SetByThreePoints(points(0), points(1), points(2))
   'Dim newUCSmat As Matrix
   'Set yourUCSmat = newUCS.Transformation
   'Call newUCSmat.Invert

    'printing the values to a csv excel sheet
    Dim dialog As FileDialog
    Dim filename As String
    Call ThisApplication.CreateFileDialog(dialog)
    With dialog
        .DialogTitle = "Specify Output .CSV File"
        .Filter = "Comma delimited file (*.csv)|*.csv"
        .FilterIndex = 0
        .OptionsEnabled = False
        .MultiSelectEnabled = False
        .ShowSave
        filename = .filename
    End With

    If filename <> "" Then
        On Error Resume Next
        Open filename For Output As #1
        If Err.Number <> 0 Then
                MsgBox "Unable to open the specified file. " & _
                "It may be open by another process."
            Exit Sub
        End If

        Dim uom As UnitsOfMeasure
        Set uom = partDoc.UnitsOfMeasure

        Print #1, "Point" & "," & _
        "X:" & "," & _
        "Y:" & "," & _
        "Z:" & "," & _
        "Radius:" & "," & _
        "Work point name:"

        '"Item: " & "," & _ partDoc.DisplayName


        For i = 0 To UBound(points)

            Dim newPoint As Point
            Set newPoint = points(i).Point

            Call newPoint.TransformBy(newUCSmat)

            Dim xCoord As Double
            xCoord = uom.ConvertUnits(newPoint.X, _
                kCentimeterLengthUnits, kInchLengthUnits)

            Dim yCoord As Double
            yCoord = uom.ConvertUnits(newPoint.Y, _
                kCentimeterLengthUnits, kInchLengthUnits)

            Dim zCoord As Double
            zCoord = uom.ConvertUnits(newPoint.Z, _
                kCentimeterLengthUnits, kInchLengthUnits)

            Print #1, i + 1 & "," & _
                Format(xCoord, "0.000") & "," & _
                Format(yCoord, "0.000") & "," & _
                Format(zCoord, "0.000") & "," & _
                Format(rad(i), "0.000") & "," & _
                points(i).Name
        Next

        Close #1
        MsgBox "Finished writing data to """ & filename & """"
    End If

End Sub

 

0 Likes
Message 7 of 8

JelteDeJong
Mentor
Mentor

Your are trying to create UserCoordinateSystemDefinition and set it in 1 line. you need to split it up like this.

Dim ucsDef As UserCoordinateSystemDefinition
Set ucsDef = partDoc.ComponentDefinition.UserCoordinateSystems.CreateDefinition
Call ucsDef.SetByThreePoints(p1, p2, p3)
partDoc.ComponentDefinition.UserCoordinateSystems.Add(ucsDef)

Also i tried to understand your code but did not manage. Can you explain why you need 2 points and at least 3 axis? For a UCS you need 3 points. the user gives you 2 and the 3e point can be created by 2 axis. why ask for more? Also i see some calculations for radii but those radii are never used or am i missing something?

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

0 Likes
Message 8 of 8

Anonymous
Not applicable

@JelteDeJong I need 2 points and at least 3 axes for the macro to work properly. Basically I have a tube with several bends in it. In order for our bending machines to know how to make them, I need to give them the radius of each bend, and the location of each bend. This macro finds all the axis a user creates and selects then iterates through them to find the intersection points and measure the radii. If there are less than 3 axes, then the tube isn't bent and it doesn't make sense to run the macro. And I'm not sure how to make the computer create the end points yet, so that's why I require 2 points (start and end) as input. 

 

I added the code you showed me, but I'm getting runtime error 424: Object Required at this line

Call ucsDef.SetByThreePoints(p1, p2, p3)

0 Likes