Insert UCS on Point

Insert UCS on Point

Luis_Pacheco_3D
Advocate Advocate
974 Views
11 Replies
Message 1 of 12

Insert UCS on Point

Luis_Pacheco_3D
Advocate
Advocate

 

    Dim doc As PartDocument = ThisApplication.ActiveDocument
    Dim sk As Sketch = doc.ComponentDefinition.Sketches.Item(doc.ComponentDefinition.Sketches.Count)
    
    If sk.SketchLines.Count > 0 Then
        For Each line As SketchLine In sk.SketchLines
            Dim startPoint As Point2d = Line.StartSketchPoint.Geometry
            Dim endPoint As Point2d = Line.EndSketchPoint.Geometry
            Dim centerPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d((startPoint.X + endPoint.X) / 2, (startPoint.Y + endPoint.Y) / 2)
            sk.SketchPoints.Add(centerPoint)
        Next
        doc.Update2()
    Else
        MsgBox("No se encontraron líneas en el boceto.")
    End If

 

Hi Everybody, I have this code to create a point in the middle of the lines in an active sketch. I need to put UCS on the created center point, I've tried but always get an error.

 

 

0 Likes
Accepted solutions (2)
975 Views
11 Replies
Replies (11)
Message 2 of 12

Andrii_Humeniuk
Advisor
Advisor

Hi @Luis_Pacheco_3D . Your code works great for me, but could be made smaller. Try it, maybe this option will work for you.

 

 

Dim doc As PartDocument = ThisApplication.ActiveDocument
Dim sk As Sketch = doc.ComponentDefinition.Sketches.Item(doc.ComponentDefinition.Sketches.Count)
sk.Edit()
If sk.SketchLines.Count > 0 Then
    For Each oLine As SketchLine In sk.SketchLines
        sk.SketchPoints.Add(oLine.Geometry.MidPoint)
    Next
Else
    MsgBox("No se encontraron líneas en el boceto.")
End If
sk.ExitEdit()
doc.Update2()

  

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 3 of 12

Luis_Pacheco_3D
Advocate
Advocate

Thanks for answering, your code works great, The problem is that I need to add a code line to insert a UCS on created point. 

0 Likes
Message 4 of 12

Andrii_Humeniuk
Advisor
Advisor

Sorry, Friday night has a negative effect on me. Here is the code that seems to do what you need.

Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oMatrix As Matrix = oTG.CreateMatrix
Dim sk As Sketch = oDef.Sketches.Item(oDef.Sketches.Count)
sk.Edit()
Dim oPoint As SketchPoint
If sk.SketchLines.Count > 0 Then
    For Each oLine As SketchLine In sk.SketchLines
        oPoint = sk.SketchPoints.Add(oLine.Geometry.MidPoint)
    Next
Else
    MsgBox("No se encontraron líneas en el boceto.")
End If
sk.ExitEdit()
oDoc.Update2()
Dim directionX As Vector = oDef.WorkAxes.Item(1).Line.Direction.AsVector
Dim directionY As Vector = oDef.WorkAxes.Item(2).Line.Direction.AsVector
Dim directionZ As Vector = oDef.WorkAxes.Item(3).Line.Direction.AsVector
oMatrix.SetCoordinateSystem(oTG.CreatePoint(oPoint.Geometry3d.X,oPoint.Geometry3d.Y,oPoint.Geometry3d.Z),directionX,directionY,directionZ)
Dim UCSDef As UserCoordinateSystemDefinition = oDef.UserCoordinateSystems.CreateDefinition()
UCSDef.Transformation = oMatrix
Dim oUCS As UserCoordinateSystem = oDef.UserCoordinateSystems.Add(UCSDef)

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 5 of 12

WCrihfield
Mentor
Mentor

Hi @Luis_Pacheco_3D.  Just so you know, to create a UCS, you need to have 3 already existing objects in the model document.  One object will be used for the Origin of the UCS, one will be used to define the X-Axis of the UCS, and the other will be used to define the Y-Axis of the UCS.  These three objects need to be one of the following:  WorkPoint, Vertex, SketchPoint, SketchPoint3D or Edge.  If an edge is supplied, its MidPoint will be used, and if the edge is an arc or circular, its center point will be used.  The MidPoint of a SketchLine, in the form of a SketchPoint could be used as the origin of the UCS, and the start point or end point of the SketchLine could also be used to define the X-Axis, but there is no pre-existing point type object for it to use to define the Y-Axis.  It wants pre-existing geometry objects so that the UCS will be constrained/attached to them.  That way if the point moves, the UCS will move with it, and stay accurate.

 

Creating the UCS the other way, by transient (math only, no geometry) locations, will not 'attach' the UCS to the real geometry, it will just place it in the right location.  Then if the point moves the UCS will stay where it was.  Just pointing that out, so you are aware.

Here is the link to the online help page for the method of the UserCoordinateSystemDefinition.SetByThreePoints.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 12

Luis_Pacheco_3D
Advocate
Advocate

@Andrii_Humeniuk  The code creates just one UCS on the last Point created, is possible to create a UCS for each of the points? (92)

 

Luis_Pacheco_3D_0-1685120203842.png

 

 

0 Likes
Message 7 of 12

Luis_Pacheco_3D
Advocate
Advocate

@WCrihfieldthanks for the clarification, the problem can be solved if I select the point first.?

0 Likes
Message 8 of 12

WCrihfield
Mentor
Mentor

Not really.  Since the sketch is 2D, and a UCS is 3D, you simply need a way to specify the second direction from the origin.  For instance of you had a sketch in the form of the capital letter "L", you have a corner where the two lines meet that could be used as the origin (a SketchPoint type object), then you could use the points at the opposite ends of the two lines going away from that origin point as the other two points (also SketchPoint objects).  All 3 points can not be on the same straight 2D line, because there is only 1 direction there.  Mid point, end point, and start point are all in line with each other on the same 2D line.  One point needs to be in another direction that is not in-line with the origin and the second point.  It's as simple as that.  Finding the existing geometry to use for that third point in many locations would be pretty challenging in a sketch that we know nothing about.  If the entire sketch was just connected straight lines, and every line was going in a new direction, we might be able to work something out with that scenario.  For example the end point of the first line could be the origin, then the start point of that first line could be used to define the X-axis, then the end point of the next line could be used for the Y-axis.  Does that make sense?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 12

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

No problem, try this:

Sub main
	Dim oDoc As PartDocument = ThisApplication.ActiveDocument
	Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	Dim sk As Sketch = oDef.Sketches.Item(oDef.Sketches.Count)
	sk.Edit()
	Dim oPoint As SketchPoint
	If sk.SketchLines.Count > 0 Then
	    For Each oLine As SketchLine In sk.SketchLines
	        oPoint = sk.SketchPoints.Add(oLine.Geometry.MidPoint)
			CreatUCS(oDef,oTG,oPoint)
	    Next
	Else
	    MsgBox("No se encontraron líneas en el boceto.")
	End If
	sk.ExitEdit()
	oDoc.Update2()
End Sub

Private Function CreatUCS(oDef As PartComponentDefinition,oTG As TransientGeometry, oPoint As SketchPoint)
	Dim oMatrix As Matrix = oTG.CreateMatrix
	Dim directionX As Vector = oDef.WorkAxes.Item(1).Line.Direction.AsVector
	Dim directionY As Vector = oDef.WorkAxes.Item(2).Line.Direction.AsVector
	Dim directionZ As Vector = oDef.WorkAxes.Item(3).Line.Direction.AsVector
	oMatrix.SetCoordinateSystem(oTG.CreatePoint(oPoint.Geometry3d.X,oPoint.Geometry3d.Y,oPoint.Geometry3d.Z),directionX,directionY,directionZ)
	Dim UCSDef As UserCoordinateSystemDefinition = oDef.UserCoordinateSystems.CreateDefinition()
	UCSDef.Transformation = oMatrix
	Dim oUCS As UserCoordinateSystem = oDef.UserCoordinateSystems.Add(UCSDef)
End Function

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 10 of 12

Luis_Pacheco_3D
Advocate
Advocate

This is my sketch, 92 lines connected.  It´s Possible?

The idea that I have is to use the @Andrii_Humeniuk code and then relocated all UCS and aligned with the respective line.

Luis_Pacheco_3D_0-1685125322275.png

 

 

 

 

0 Likes
Message 11 of 12

Luis_Pacheco_3D
Advocate
Advocate

Thanks a lot, @Andrii_Humeniuk , that works for me, now I will try to solve the problem that @WCrihfield mentioned.

0 Likes
Message 12 of 12

Luis_Pacheco_3D
Advocate
Advocate
Accepted solution

I edited @Andrii_Humeniuk  code and it works perfectly for what I'm looking for.

 

 

 

Sub main
    Dim oDoc As PartDocument = ThisApplication.ActiveDocument
    Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
    Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
    Dim sk As Sketch = oDef.Sketches.Item(oDef.Sketches.Count)
    sk.Edit()
    Dim oPoint As SketchPoint
	 Dim StarLine As SketchPoint
	 Dim EndLine As SketchPoint
    If sk.SketchLines.Count > 0 Then
        For Each oLine As SketchLine In sk.SketchLines
		StarLine = oLine.StartSketchPoint
		 EndLine =  oLine.EndSketchPoint
			oPoint = sk.SketchPoints.Add(oLine.Geometry.MidPoint)
			sk.GeometricConstraints.AddMidpoint(oPoint, oLine)
			oUCSDef = oDef.UserCoordinateSystems.CreateDefinition
			oUCSDef.SetByThreePoints(oPoint, oLine.StartSketchPoint, oLine.EndSketchPoint)
			 oUCS = oDef.UserCoordinateSystems.Add(oUCSDef)
			 oUCS.YAngle.Value = (90 * (PI / 180))
              oUCS.ZAngle.Value = (90 * (PI / 180))
        Next
    Else
        MsgBox("No se encontraron líneas en el boceto.")
    End If
    sk.ExitEdit()
    oDoc.Update2()
End Sub

 

 

I post them here in case anyone needs it

0 Likes