Projecting drawing symbol points to drawing level sketch

demuff
Enthusiast

Projecting drawing symbol points to drawing level sketch

demuff
Enthusiast
Enthusiast

I need to be able to project the connection points from sketched symbols to a drawing sketch. Since we can't project geometry from sketched symbols I fleshed out some code to do it. I know it won't be linked the the symbol entity but it's better than nothing.

 

The problem now is that the SketchToSheetSpace and SheetToSketchSpace methods of the drawing sketch object don't change the sketch coordinates to sheet coordinates, so the points that get added don't line up with the connection points on the symbols. I don't get any errors, there's just no coordinate conversion.

 

Do these methods work for sketched symbol drawings? Thanks!

 

Sub projectSymbol()

    Dim oDwg As DrawingDocument
    Set oDwg = ThisApplication.ActiveDocument
    Dim oSht As Sheet
    Set oSht = oDwg.ActiveSheet
    Dim symSketch As DrawingSketch
    Dim oSketch As DrawingSketch
    Dim sketchName As String
    sketchName = "SymbolSketch"
    
    'Get or create the symbol sketch -----
    For Each oSketch In oSht.Sketches
        If oSketch.name = sketchName Then
            Set symSketch = oSketch
            Exit For
        End If
    Next
    
    If symSketch Is Nothing Then
        Set symSketch = oSht.Sketches.Add
        symSketch.name = sketchName
    End If
    '------------
    symSketch.Edit
    
    'add unique symbol connection points to sketch ------
    Dim oSym As SketchedSymbol
    Dim oSymPoint As SketchPoint
    Dim oPoint As Variant
    Dim ptColl As Collection: Set ptColl = New Collection
    Dim flag As Boolean
    Dim i
    Dim newPointDef As Point2d
    Dim newPoint As SketchEntity
    
    For Each oSym In oSht.SketchedSymbols
        For Each oSymPoint In oSym.Definition.Sketch.SketchPoints
            If oSymPoint.ConnectionPoint = True Then 'only transfer connection points
                flag = True 'reset the flag
                Set newPointDef = oSym.Definition.Sketch.SketchToSheetSpace(oSymPoint.Geometry)
                Debug.Print "Symbol Sketch: " & oSymPoint.Geometry.X & " | " & oSymPoint.Geometry.Y
                Debug.Print "New Point: " & newPointDef.X & " | " & newPointDef.Y & vbCrLf
                
                For i = 1 To ptColl.Count
                    Set oPoint = ptColl.Item(i)
                    If oPoint.X = oSymPoint.Geometry.X And oPoint.Y = oSymPoint.Geometry.Y Then
                        flag = False
                        Exit For
                    End If
                Next
                If flag Then 'only add point if not duplicate
                    Set newPoint = symSketch.SketchPoints.Add(symSketch.SheetToSketchSpace(newPointDef))  'create the new point
                    Call symSketch.GeometricConstraints.AddGround(newPoint) 'ground the new point
                    Call ptColl.Add(oSymPoint.Geometry) 'save the point in the collection
                End If
            End If
        Next
    Next
    '-----------

    symSketch.ExitEdit
End Sub
0 Likes
Reply
Accepted solutions (1)
853 Views
7 Replies
Replies (7)

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi Derek,

 

Could you please provide sample drawing file with sketched symbols (non confidential) ?

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes

frederic.vandenplas
Collaborator
Collaborator

Hi,

 

You are refering to the sketched symbol, but the measurement you get is the x & y distance of the sketchedsymboldefinition

(0,0 vs insertionpoint)

If i create the insertionpoint on X 0 , Y 0 and place the block on the lower left corner, the point is created at the expected location.

BUT inventor calculate this point from the lower left corner of the border (as you can see in the image)

It needs a little bit of math, to get it correct, but i hope with this tip it wil work for you

Knipsel.JPG

If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes

demuff
Enthusiast
Enthusiast

Thank you for the suggestion. I attempted to do transform the coordinates manually first, however I haven't been able to get the sketch position for symbols that don't have a discrete insertion point (many of the ones I am working with do not). In this instance the sketch.position property appears to be of an approximate centroid rather than a point on the sketch from which the offset can be determined.

 

Likewise, since I'm working with a perpetually growing library of symbols from different users, it is generally not feasible to manage the insertion point and origin of every symbol.

 

Here is a generic drawing example as well.

0 Likes

frederic.vandenplas
Collaborator
Collaborator

Hi, i've digged a little bit deeper (quick and dirtyand stripped everything you had) but it's working!

It will project the first sketched symbol insertion point. You'll need to modify it to your needs...

 

Sub projectSymbol()

    Dim oDwg As DrawingDocument
    Set oDwg = ThisApplication.ActiveDocument
    Dim oSht As Sheet
    Set oSht = oDwg.ActiveSheet
    Dim symSketch As DrawingSketch
    Dim oSketch As DrawingSketch
    Dim sketchName As String
    sketchName = "SymbolSketch"
    
    'Get or create the symbol sketch -----
    For Each oSketch In oSht.Sketches
        If oSketch.Name = sketchName Then
            Set symSketch = oSketch
            Exit For
        End If
    Next
    
    If symSketch Is Nothing Then
        Set symSketch = oSht.Sketches.Add
        symSketch.Name = sketchName
    End If
    '------------
    symSketch.Edit
    
    'add unique symbol connection points to sketch ------
    Dim oSym As SketchedSymbol
    Dim oSymPoint As SketchPoint
    Dim oPoint As Variant
    Dim ptColl As Collection: Set ptColl = New Collection
    Dim flag As Boolean
    Dim i
    Dim newPointDef As Point2d
    Dim newPoint As SketchEntity
    Set newPointDef = ThisApplication.TransientGeometry.CreatePoint2d(oSht.SketchedSymbols(1).Position.x, oSht.SketchedSymbols(1).Position.Y)
       
   
                    Set newPoint = symSketch.SketchPoints.Add(symSketch.SheetToSketchSpace(newPointDef))  'create the new point
                    Call symSketch.GeometricConstraints.AddGround(newPoint) 'ground the new point
                   

    symSketch.ExitEdit
End Sub
If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes

demuff
Enthusiast
Enthusiast

I've been able to project the insertion point (the position property as you demonstrated in your code) but I am actually looking for the connection points/grips. In the example dwg I provided I added a connection point to the end of the line outside the circle. My original code projects that point but at the wrong position. What I was trying to say in my previous response was that I don't see how to use the position property alone to determine the offset of the connection point for cases where the symbol doesn't have an explicit insertion point.

 

inv_pts.png

 

 

0 Likes

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi Derek,

 

Few suggestions on sketched symbols given in the example.dwg. Insertion point moved to center of circle as shown below.

 

Insertion point moved.png

 

The modified sample drawing attached with this post (Opens in Inventor 2017 and above).

 

Connection point can not project directly. but remapped or interpolated with insertion point of sketched symbol.

 

Please find the following VBA code which projects both insertion point and connection point after above modification.

 

Sub projectSymbol()

    Dim oDwg As DrawingDocument
    Set oDwg = ThisApplication.ActiveDocument
    
    Dim oSht As Sheet
    Set oSht = oDwg.ActiveSheet
    
    Dim connPt As Point2d
    Dim insertPt As Point2d

    Dim def As SketchedSymbolDefinition
    Set def = oSht.SketchedSymbols.Item(1).Definition
    
    Dim pt As SketchPoint

    For Each pt In def.Sketch.SketchPoints
        If pt.ConnectionPoint = True Then
            Set connPt = def.Sketch.SheetToSketchSpace(pt.Geometry)
        ElseIf pt.InsertionPoint = True Then
            Set insertPt = def.Sketch.SheetToSketchSpace(pt.Geometry)
        End If
    Next

    Dim diffX As Double
    diffX = connPt.X - insertPt.X

    Dim diffY As Double
    diffY = connPt.Y - insertPt.Y
    
    Dim symSketch As DrawingSketch
    Dim oSketch As DrawingSketch
    
    Dim sketchName As String
    sketchName = "SymbolSketch"
    
    'Get or create the symbol sketch -----
    For Each oSketch In oSht.Sketches
        If oSketch.Name = sketchName Then
            Set symSketch = oSketch
            Exit For
        End If
    Next
    
    If symSketch Is Nothing Then
        Set symSketch = oSht.Sketches.Add
        symSketch.Name = sketchName
    End If
    '------------
    symSketch.Edit
    
    'add unique symbol connection points to sketch ------
    
    Dim inPt As Point2d
    Set inPt = ThisApplication.TransientGeometry.CreatePoint2d(oSht.SketchedSymbols(1).Position.X, oSht.SketchedSymbols(1).Position.Y)
    
    Dim insertionPt As SketchEntity
    Set insertionPt = symSketch.SketchPoints.Add(symSketch.SheetToSketchSpace(inPt))  'create the new point
    Call symSketch.GeometricConstraints.AddGround(insertionPt) 'ground the new point
    
    Dim coPt As Point2d
    Set coPt = ThisApplication.TransientGeometry.CreatePoint2d(oSht.SketchedSymbols(1).Position.X + diffX, oSht.SketchedSymbols(1).Position.Y + diffY)
    
    Dim connectionPt As SketchEntity
    Set connectionPt = symSketch.SketchPoints.Add(symSketch.SheetToSketchSpace(coPt))  'create the connection point
    Call symSketch.GeometricConstraints.AddGround(connectionPt) 'ground the connection point

    symSketch.ExitEdit
    
End Sub

Please feel free to contact if there is any doubt.

 

If solves your problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes

demuff
Enthusiast
Enthusiast

Thanks Chandra.

I arrived at a similar solution myself, using the insertion point offset to adjust the connection point. It would be nice to be able to do this on a generic symbol without a defined insertion point but this method will generally work.

0 Likes