Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
Anonymous
326 Views, 1 Reply

Insert symbol, if it is not there

I need a way to check if a symbol is already in a drawing before running this following code that inserts a symbol

SyntaxEditor Code Snippet

' Set a reference to the drawing document.
    ' This assumes a drawing document is active.
    Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument

    ' Obtain a reference to the desired sketched symbol definition.
    Dim oSketchedSymbolDef As SketchedSymbolDefinition = oDrawDoc.SketchedSymbolDefinitions.Item("MATERIAL")

    Dim oSheet As Sheet = oDrawDoc.ActiveSheet

    ' This sketched symbol definition contains one prompted string input. An array
    ' must be input that contains the strings for the prompted strings.
    Dim oPromptStrings As String() = {iProperties.Value("Project", "STOCK NUMBER"),""}
    


    Dim oBorder As Border
    oBorder = oSheet.Border
    Dim oPlacePoint As Point2d
    oyOffset = 7 'user defined offset
    oxOffset = 9
    oX = oBorder.RangeBox.MaxPoint.X - oxOffset
    oY = oBorder.RangeBox.MinPoint.Y + oyOffset
    oPlacePoint = ThisApplication.TransientGeometry.CreatePoint2d(oX,oY)
    ' Add an instance of the sketched symbol definition to the sheet.
    ' Rotate the instance by 45 degrees and scale by .75 when adding.
    ' The symbol will be inserted at (0,0) on the sheet. Since the
    ' start point of the line was marked as the insertion point, the
    ' start point should end up at (0,0).
    Dim oSketchedSymbol As SketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oPlacePoint, (0), 1,oPromptStrings )

bradeneuropeArthur
in reply to: Anonymous

With this...

 

' Set a reference to the drawing document.
    ' This assumes a drawing document is active.
    Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument

    ' Obtain a reference to the desired sketched symbol definition.
    Dim oSketchedSymbolDef As SketchedSymbolDefinition = oDrawDoc.SketchedSymbolDefinitions.Item("MATERIAL")

    Dim oSheet As Sheet = oDrawDoc.ActiveSheet

    ' This sketched symbol definition contains one prompted string input. An array
    ' must be input that contains the strings for the prompted strings.
    Dim oPromptStrings As String() = {iProperties.Value("Project", "STOCK NUMBER"),""}
    
Dim sk As SketchedSymbol
For Each sk In oSheet.SketchedSymbols


If sk.Name = "MATERIAL" Then
Exit Sub
End If

Next

    Dim oBorder As Border
    oBorder = oSheet.Border
    Dim oPlacePoint As Point2d
    oyOffset = 7 'user defined offset
    oxOffset = 9
    oX = oBorder.RangeBox.MaxPoint.X - oxOffset
    oY = oBorder.RangeBox.MinPoint.Y + oyOffset
    oPlacePoint = ThisApplication.TransientGeometry.CreatePoint2d(oX,oY)
    ' Add an instance of the sketched symbol definition to the sheet.
    ' Rotate the instance by 45 degrees and scale by .75 when adding.
    ' The symbol will be inserted at (0,0) on the sheet. Since the
    ' start point of the line was marked as the insertion point, the
    ' start point should end up at (0,0).
    Dim oSketchedSymbol As SketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oPlacePoint, (0), 1,oPromptStrings )

Regards,

Arthur Knoors

Autodesk Affiliations:

Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!


! For administrative reasons, please mark a "Solution as solved" when the issue is solved !