anyone know why did code fail something ?

anyone know why did code fail something ?

Darkforce_the_ilogic_guy
Advisor Advisor
210 Views
2 Replies
Message 1 of 3

anyone know why did code fail something ?

Darkforce_the_ilogic_guy
Advisor
Advisor

anyone know why did code fail something ?  I create this code all time ago. and most of the time it work just fine , but somtimes I get this error.  

 

 

Darkforce_the_ilogic_guy_0-1706099467565.png

it seens to not happen all the time , not even when it run on the same file

 

' New
Dim oDrawDoc As DrawingDocument = ThisDrawing.Document



'Dim oDrawDoc As DrawingDocument = C:\Working Folder\CAD\Inventor 2020\Design Data\Symbol Library\Standard Text.idw
 
' Obtain a reference to the desired sketched symbol definition.
Dim oSketchedSymbolDef As SketchedSymbolDefinition _
       '= oDrawDoc.SketchedSymbolDefinitions.Item("DraftA4")
	   
	   
 'oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA4")

Dim oSheet As Sheet = oDrawDoc.ActiveSheet

If oSheet.Size.ToString = "kA4DrawingSheetSize" Then
	Try
	oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA4")
	'delete if not working
	Catch
		
		Dim oDrawing As DrawingDocument
oDrawing = ThisApplication.ActiveDocument

Dim oSketchSym As SketchedSymbol
Dim oSketchSymLib As SketchedSymbolDefinitionLibrary
oSketchSymLib = oDrawing.SketchedSymbolDefinitions.SketchedSymbolDefinitionLibraries.Item("Standard Text.idw")


Dim oSSDEf As SketchedSymbolDefinition



oSSDEf = oDrawing.SketchedSymbolDefinitions.AddFromLibrary(oSketchSymLib, "DraftA4", True)
		oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA4")
	End Try
	'delete if not working
Else If oSheet.Size.ToString = "kA3DrawingSheetSize" Then
	Try
	oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA3")
	
	
	'delete if not working
	Catch
		
		Dim oDrawing As DrawingDocument
oDrawing = ThisApplication.ActiveDocument

Dim oSketchSym As SketchedSymbol
Dim oSketchSymLib As SketchedSymbolDefinitionLibrary
oSketchSymLib = oDrawing.SketchedSymbolDefinitions.SketchedSymbolDefinitionLibraries.Item("Standard Text.idw")


Dim oSSDEf As SketchedSymbolDefinition
oSSDEf = oDrawing.SketchedSymbolDefinitions.AddFromLibrary(oSketchSymLib, "DraftA3", True)
		oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA3")
	End Try
	'delete if not working
Else If oSheet.Size.ToString = "kA2DrawingSheetSize" Then
	Try
	oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA2")
	'delete if not working
	Catch
		
		Dim oDrawing As DrawingDocument
oDrawing = ThisApplication.ActiveDocument

Dim oSketchSym As SketchedSymbol
Dim oSketchSymLib As SketchedSymbolDefinitionLibrary
oSketchSymLib = oDrawing.SketchedSymbolDefinitions.SketchedSymbolDefinitionLibraries.Item("Standard Text.idw")


Dim oSSDEf As SketchedSymbolDefinition
oSSDEf = oDrawing.SketchedSymbolDefinitions.AddFromLibrary(oSketchSymLib, "DraftA2", True)
		oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA2")
	End Try
	'delete if not working
Else If oSheet.Size.ToString = "kA1DrawingSheetSize" Then
	Try
	oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA1")
	'delete if not working
	Catch
		
		Dim oDrawing As DrawingDocument
oDrawing = ThisApplication.ActiveDocument

Dim oSketchSym As SketchedSymbol
Dim oSketchSymLib As SketchedSymbolDefinitionLibrary
oSketchSymLib = oDrawing.SketchedSymbolDefinitions.SketchedSymbolDefinitionLibraries.Item("Standard Text.idw")


Dim oSSDEf As SketchedSymbolDefinition
oSSDEf = oDrawing.SketchedSymbolDefinitions.AddFromLibrary(oSketchSymLib, "DraftA1", True)
		oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA1")
	End Try
	'delete if not working
	
End If 
 
'create insertion point, coordinates - in cm !
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oInsertionPoint As Point2d = oTG.CreatePoint2d(1, 19)
 
' Add an instance of the sketched symbol definition to the sheet.
' Rotate angle = 0 radians,
' scale = 1 when adding
' no prompt text
Dim oSketchedSymbol As SketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oInsertionPoint, (4 * Atan(1) * 0 / 180), 1, Nothing)

 

0 Likes
211 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor

Hi @Darkforce_the_ilogic_guy.  I cleaned up your code a bit, because it had a lot of repeated stuff, and also redefined the drawing document a bunch of times.  But initially one of the details that caught my eye was at then end of Line 9.  You were using the " _" at the end of that line, which indicates that you want to wrap this line of code to the next line.  But if you use that, then there is nothing useful on that very next line, it causes problems.  I also highly doubt that you really need two different variables (oSSDEf and oSketchedSymbolDef) for representing the same object, and I doubt that you actually need to obtain that same SketchedSymbolDefinition twice for every size.  The first line, where it is copying it from the library to the local document, and replacing any existing one, should be good enough, and should leave you with a reference to the 'local' copy of it, not the library copy.  The line after that for getting the one from the document, and setting that to a different variable is likely unnecessary.  So, this code could likely be reduces in size yet again, if you wanted.

Dim oDrawDoc As DrawingDocument = ThisDrawing.Document
Dim oSketchSymLib As SketchedSymbolDefinitionLibrary
oSketchSymLib = oDrawDoc.SketchedSymbolDefinitions.SketchedSymbolDefinitionLibraries.Item("Standard Text.idw")
'Dim sSketchedSymbolLibraryFile As String = "C:\Working Folder\CAD\Inventor 2020\Design Data\Symbol Library\Standard Text.idw"
Dim oSSDEf As SketchedSymbolDefinition
Dim oSketchedSymbolDef As SketchedSymbolDefinition
 Dim oSheet As Sheet = oDrawDoc.ActiveSheet

If oSheet.Size = DrawingSheetSizeEnum.kA4DrawingSheetSize Then
	Try
		oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA4")
	Catch
		oSSDEf = oDrawDoc.SketchedSymbolDefinitions.AddFromLibrary(oSketchSymLib, "DraftA4", True)
		oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA4")
	End Try
ElseIf oSheet.Size = DrawingSheetSizeEnum.kA3DrawingSheetSize Then
	Try
		oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA3")
	Catch
		oSSDEf = oDrawDoc.SketchedSymbolDefinitions.AddFromLibrary(oSketchSymLib, "DraftA3", True)
		oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA3")
	End Try
ElseIf oSheet.Size = DrawingSheetSizeEnum.kA2DrawingSheetSize Then
	Try
		oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA2")
	Catch
		oSSDEf = oDrawDoc.SketchedSymbolDefinitions.AddFromLibrary(oSketchSymLib, "DraftA2", True)
		oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA2")
	End Try
Else If oSheet.Size = DrawingSheetSizeEnum.kA1DrawingSheetSize Then
	Try
		oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA1")
	Catch
		oSSDEf = oDrawDoc.SketchedSymbolDefinitions.AddFromLibrary(oSketchSymLib, "DraftA1", True)
		oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("DraftA1")
	End Try
End If 
 
'create insertion point, coordinates - in cm !
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oInsertionPoint As Point2d = oTG.CreatePoint2d(1, 19)
 
' Add an instance of the sketched symbol definition to the sheet.
' Rotate angle = 0 radians,
' scale = 1 when adding
' no prompt text
Dim oSketchedSymbol As SketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oInsertionPoint, (4 * Atan(1) * 0 / 180), 1, Nothing)

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

rossano_praderi
Collaborator
Collaborator

I would like to suggest you to make your code more shorter using "Select Case ... End Select" instead "If ... ElseIf..End If"

 

 

		Dim oDrawDoc As DrawingDocument = ThisDrawing.Document
		Dim oSketchSymLib As SketchedSymbolDefinitionLibrary
		oSketchSymLib = oDrawDoc.SketchedSymbolDefinitions.SketchedSymbolDefinitionLibraries.Item("Standard Text.idw")
		'Dim sSketchedSymbolLibraryFile As String = "C:\Working Folder\CAD\Inventor 2020\Design Data\Symbol Library\Standard Text.idw"
		Dim oSSDEf As SketchedSymbolDefinition
		Dim oSketchedSymbolDef As SketchedSymbolDefinition
		Dim oSheet As Sheet = oDrawDoc.ActiveSheet
		
		Dim s As String = ""
		Select Case oSheet.Size
		Case DrawingSheetSizeEnum.kA4DrawingSheetSize
			s = "DraftA4"
		Case DrawingSheetSizeEnum.kA3DrawingSheetSize
			s = "DraftA3"
		Case DrawingSheetSizeEnum.kA2DrawingSheetSize
			s = "DraftA2"
		Case DrawingSheetSizeEnum.kA1DrawingSheetSize
			s = "DraftA1"
		Case DrawingSheetSizeEnum.kA0DrawingSheetSize
			s = "DraftA0"
		End Select

		Try
			oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item(s)
		Catch
			oSSDEf = oDrawDoc.SketchedSymbolDefinitions.AddFromLibrary(oSketchSymLib, s, True)
			oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item(s)
		End Try
		 
		'create insertion point, coordinates - in cm !
		Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
		Dim oInsertionPoint As Point2d = oTG.CreatePoint2d(1, 19)

		' Add an instance of the sketched symbol definition to the sheet.
		' Rotate angle = 0 radians,
		' scale = 1 when adding
		' no prompt text
		Dim oSketchedSymbol As SketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oInsertionPoint, (4 * Atan(1) * 0 / 180), 1, Nothing)

 

 



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------