insert block into drawing sheet with custom part property

insert block into drawing sheet with custom part property

Miguel.CarvajalS34Q6
Advocate Advocate
1,079 Views
15 Replies
Message 1 of 16

insert block into drawing sheet with custom part property

Miguel.CarvajalS34Q6
Advocate
Advocate

I have a custom property that I can change from a list on a part form.
This variable must be recognized at the time of inserting the part into the drawing sheet, depending on this variable, which can be N7, N5, N4, N3, etc. A predefined block that already exists in the resources folder of the drawing module will be inserted.
The custom property has the same names as the blocks in the resources folder, so when you select the property in the part, in the drawing format it will recognize the block and insert it in a coordinate dependent on the size of the sheet.

 

"

InventorVb.DocumentUpdate()
Dim BLOQUE As String = iProperties.Value("Custom", "RUGOSIDAD")
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet

Dim oSketchedSymbolDef As SketchedSymbolDefinition
oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("RUGOSIDAD")

Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry


Select Case BLOQUE
    Case "FORMATO A4"
        Dim oSketchedSymbol As SketchedSymbol
oSketchedSymbol = oSheet.SketchedSymbols.Add _
	(oSketchedSymbolDef, oTG.CreatePoint2d(11.3, 6.2), 0, 1, sPromptStrings)

    Case "FORMATO A3"
       Dim oSketchedSymbol As SketchedSymbol
oSketchedSymbol = oSheet.SketchedSymbols.Add _
	(oSketchedSymbolDef, oTG.CreatePoint2d(15.5, 6.2), 0, 1, sPromptStrings)
	
	 Case "FORMATO A2"
       Dim oSketchedSymbol As SketchedSymbol
oSketchedSymbol = oSheet.SketchedSymbols.Add _
	(oSketchedSymbolDef, oTG.CreatePoint2d(17.5, 6.2), 0, 1, sPromptStrings)
	
	 Case "FORMATO A1"
       Dim oSketchedSymbol As SketchedSymbol
oSketchedSymbol = oSheet.SketchedSymbols.Add _
	(oSketchedSymbolDef, oTG.CreatePoint2d(20.5, 6.2), 0, 1, sPromptStrings)
	
	 Case "FORMATO A0"
       Dim oSketchedSymbol As SketchedSymbol
oSketchedSymbol = oSheet.SketchedSymbols.Add _
	(oSketchedSymbolDef, oTG.CreatePoint2d(23.5, 6.2), 0, 1, sPromptStrings)

    Case Else
        
End Select


oSheet.Update()

formato a dibujo.jpg

 I generated this code, but I still can't get it to work

I don't know if what I want to achieve is possible, I can make a block be inserted automatically into the drawing, but I can't make the block selection exercise more dynamic.
can you help me, please

0 Likes
1,080 Views
15 Replies
Replies (15)
Message 2 of 16

yuzeaa
Advocate
Advocate

This can be done, but it's a bit complicated and requires the use of UserInputEvents. When I have free time, I can help you complete this task

0 Likes
Message 3 of 16

Miguel.CarvajalS34Q6
Advocate
Advocate

ok, thanks

0 Likes
Message 4 of 16

yuzeaa
Advocate
Advocate

I roughly wrote an initial version for you to test; feel free to make any adjustments if there are issues.

Class ThisRule
	Private WithEvents oApplicationEvents As ApplicationEvents
	Dim preSheetSize As String
	Dim isFirstView As Boolean = False
	Sub main
		If SharedVariable.Exists("DocumengChangeEvent") Then Return
		oApplicationEvents = ThisApplication.ApplicationEvents
		SharedVariable("DocumengChangeEvent") = "DocumengChangeEvent"
	End Sub
    Private Sub drawingChangeEvent( DocumentObject As _Document,
                                    BeforeOrAfter As EventTimingEnum,
                                    ReasonsForChange As CommandTypesEnum,
                                    Context As NameValueMap,
                                    ByRef HandlingCode As HandlingCodeEnum) _
									Handles oApplicationEvents.OnDocumentChange
        HandlingCode = HandlingCodeEnum.kEventNotHandled
		If BeforeOrAfter = EventTimingEnum.kBefore Then
			If Context(1) = "ChangeSheetProperties" Then
				preSheetSize = DocumentObject.ActiveSheet.Size
			Else If Context(1) = "EditDrawingBaseView" Then
			   If DocumentObject.ActiveSheet.DrawingViews.count = 0 Then
				   isFirstView = True
			   End If 
			End If
        End If
        If BeforeOrAfter = EventTimingEnum.kAfter Then
			If Context(1) = "ChangeSheetProperties" Then
				If DocumentObject.ActiveSheet.Size <> preSheetSize Then
					repositionsymbol(DocumentObject)
				End If
			Else If Context(1) = "EditDrawingBaseView" Then
				If isFirstView Then
					isFirstView = False
					Dim doc As DrawingDocument = DocumentObject
					Dim modeldoc As Document = doc.ActiveSheet.DrawingViews(1).ReferencedDocumentDescriptor.ReferencedDocument
					Try
					 Dim BLOQUE As String = modeldoc.PropertySets("Inventor User Defined Properties")("RUGOSIDAD").Value
					 addSketchsymbol(doc,BLOQUE)
					Catch
					 Exit Sub
					End Try
				End If
			 End If 
        End If
    End Sub
	
	Sub addSketchsymbol(oDrawDoc As DrawingDocument, blockname As String)
	   Dim oSheet As Sheet
       oSheet = oDrawDoc.ActiveSheet
	   Dim oSketchedSymbolDef As SketchedSymbolDefinition
       Try
	   oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item(blockname)
	   Catch
	   Exit Sub
	   End Try
	   Dim oTG As TransientGeometry
       oTG = ThisApplication.TransientGeometry
	   Dim sPromptStrings(0) As String
	   sPromptStrings(0) = "str"
	   oSketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oTG.CreatePoint2d(17.5, 6.2), 0, 1, sPromptStrings)
	   oSketchedSymbol.Name = "FirstSketchSymbol"
	   repositionsymbol(oDrawDoc)
	End Sub
	
	Sub repositionsymbol(oDrawDoc As DrawingDocument)
	   Dim oSheet As Sheet
       oSheet = oDrawDoc.ActiveSheet
	   Dim SketchedSymbol As SketchedSymbol
	   Try
		   SketchedSymbol = oSheet.SketchedSymbols("FirstSketchSymbol")
	   Catch
		   Exit Sub
	   End Try
	   Dim oTG As TransientGeometry
       oTG = ThisApplication.TransientGeometry
	   Select Case oSheet.Size
	       Case DrawingSheetSizeEnum.kA0DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(23.5, 6.2)
		   Case DrawingSheetSizeEnum.kA1DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(20.5, 6.2)
		   Case DrawingSheetSizeEnum.kA2DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(17.5, 6.2)
		   Case DrawingSheetSizeEnum.kA3DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(15.5, 6.2)
		   Case DrawingSheetSizeEnum.kA4DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(11.3, 6.2)
	   End Select
	End Sub
End Class
Message 5 of 16

Miguel.CarvajalS34Q6
Advocate
Advocate

The code is very interesting and I appreciate the interest and how it proposes the solution, but I have tried to run the code and it does not execute any task, I try to relate the variables in a better way but it still does not perform any task.

I share the files with you so you can better understand what is happening.
thank you so much

0 Likes
Message 6 of 16

yuzeaa
Advocate
Advocate

I am using Inventor2020 and can't open your file. what is the property name of ipt,what is the promptstring of block.

0 Likes
Message 7 of 16

Miguel.CarvajalS34Q6
Advocate
Advocate

the custom property is called "RUGGESNESS" and the block on the drawing sheet is called "N7"

0 Likes
Message 8 of 16

yuzeaa
Advocate
Advocate
Class ThisRule
	Private WithEvents oApplicationEvents As ApplicationEvents
	Dim preSheetSize As String
	Dim isFirstView As Boolean = False
	Sub main
		If SharedVariable.Exists("DocumengChangeEvent") Then Return
		oApplicationEvents = ThisApplication.ApplicationEvents
		SharedVariable("DocumengChangeEvent") = "DocumengChangeEvent"
	End Sub
    Private Sub drawingChangeEvent( DocumentObject As _Document,
                                    BeforeOrAfter As EventTimingEnum,
                                    ReasonsForChange As CommandTypesEnum,
                                    Context As NameValueMap,
                                    ByRef HandlingCode As HandlingCodeEnum) _
									Handles oApplicationEvents.OnDocumentChange
        HandlingCode = HandlingCodeEnum.kEventNotHandled
		If BeforeOrAfter = EventTimingEnum.kBefore Then
			If Context(1) = "ChangeSheetProperties" Then
				preSheetSize = DocumentObject.ActiveSheet.Size
			Else If Context(1) = "EditDrawingBaseView" Then
			   If DocumentObject.ActiveSheet.DrawingViews.count = 0 Then
				   isFirstView = True
			   End If 
			End If
        End If
        If BeforeOrAfter = EventTimingEnum.kAfter Then
			If Context(1) = "ChangeSheetProperties" Then
				If DocumentObject.ActiveSheet.Size <> preSheetSize Then
					repositionsymbol(DocumentObject)
				End If
			Else If Context(1) = "EditDrawingBaseView" Then
				If isFirstView Then
					isFirstView = False
					Dim doc As DrawingDocument = DocumentObject
					Dim modeldoc As Document = doc.ActiveSheet.DrawingViews(1).ReferencedDocumentDescriptor.ReferencedDocument
					Try
					 Dim BLOQUE As String = modeldoc.PropertySets("Inventor User Defined Properties")("RUGGESNESS").Value
					 addSketchsymbol(doc,BLOQUE)
					Catch
					 Exit Sub
					End Try
				End If
			 End If 
        End If
    End Sub
	
	Sub addSketchsymbol(oDrawDoc As DrawingDocument, blockname As String)
	   Dim oSheet As Sheet
       oSheet = oDrawDoc.ActiveSheet
	   Dim oSketchedSymbolDef As SketchedSymbolDefinition
       Try
	   oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item(blockname)
	   Catch
	   Exit Sub
	   End Try
	   Dim oTG As TransientGeometry
       oTG = ThisApplication.TransientGeometry
	   oSketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oTG.CreatePoint2d(17.5, 6.2))
	   oSketchedSymbol.Name = "FirstSketchSymbol"
	   repositionsymbol(oDrawDoc)
	End Sub
	
	Sub repositionsymbol(oDrawDoc As DrawingDocument)
	   Dim oSheet As Sheet
       oSheet = oDrawDoc.ActiveSheet
	   Dim SketchedSymbol As SketchedSymbol
	   Try
		   SketchedSymbol = oSheet.SketchedSymbols("FirstSketchSymbol")
	   Catch
		   Exit Sub
	   End Try
	   Dim oTG As TransientGeometry
       oTG = ThisApplication.TransientGeometry
	   Select Case oSheet.Size
	       Case DrawingSheetSizeEnum.kA0DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(23.5, 6.2)
		   Case DrawingSheetSizeEnum.kA1DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(20.5, 6.2)
		   Case DrawingSheetSizeEnum.kA2DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(17.5, 6.2)
		   Case DrawingSheetSizeEnum.kA3DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(15.5, 6.2)
		   Case DrawingSheetSizeEnum.kA4DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(11.3, 6.2)
	   End Select
	End Sub
End Class
0 Likes
Message 9 of 16

Miguel.CarvajalS34Q6
Advocate
Advocate

I sent you some images of the intention I have with the code, maybe you will understand me better

0 Likes
Message 10 of 16

yuzeaa
Advocate
Advocate

The information you provided in your previous post was incorrect.  Try it again with my last post

20231215230050.png

20231215230931.png

 

 

0 Likes
Message 11 of 16

yuzeaa
Advocate
Advocate

Sorry, I didn't see clearly that you were using parameters. I will correct it and post it later.

0 Likes
Message 12 of 16

yuzeaa
Advocate
Advocate

20231215232356.png

Class ThisRule
	Private WithEvents oApplicationEvents As ApplicationEvents
	Dim preSheetSize As String
	Dim isFirstView As Boolean = False
	Sub main
		If SharedVariable.Exists("DocumengChangeEvent") Then Return
		oApplicationEvents = ThisApplication.ApplicationEvents
		SharedVariable("DocumengChangeEvent") = "DocumengChangeEvent"
	End Sub
    Private Sub drawingChangeEvent( DocumentObject As _Document,
                                    BeforeOrAfter As EventTimingEnum,
                                    ReasonsForChange As CommandTypesEnum,
                                    Context As NameValueMap,
                                    ByRef HandlingCode As HandlingCodeEnum) _
									Handles oApplicationEvents.OnDocumentChange
        HandlingCode = HandlingCodeEnum.kEventNotHandled
		If BeforeOrAfter = EventTimingEnum.kBefore Then
			If Context(1) = "ChangeSheetProperties" Then
				preSheetSize = DocumentObject.ActiveSheet.Size
			Else If Context(1) = "EditDrawingBaseView" Then
			   If DocumentObject.ActiveSheet.DrawingViews.count = 0 Then
				   isFirstView = True
			   End If 
			End If
        End If
        If BeforeOrAfter = EventTimingEnum.kAfter Then
			If Context(1) = "ChangeSheetProperties" Then
				If DocumentObject.ActiveSheet.Size <> preSheetSize Then
					repositionsymbol(DocumentObject)
				End If
			Else If Context(1) = "EditDrawingBaseView" Then
				If isFirstView Then
					isFirstView = False
					Dim doc As DrawingDocument = DocumentObject
					Dim modeldoc As Document = doc.ActiveSheet.DrawingViews(1).ReferencedDocumentDescriptor.ReferencedDocument
					Try
					 Dim BLOQUE As String = modeldoc.ComponentDefinition.Parameters("RUGOSIDAD").Value
					 addSketchsymbol(doc,BLOQUE)
					Catch
					 Exit Sub
					End Try
				End If
			 End If 
        End If
    End Sub
	
	Sub addSketchsymbol(oDrawDoc As DrawingDocument, blockname As String)
	   Dim oSheet As Sheet
       oSheet = oDrawDoc.ActiveSheet
	   Dim oSketchedSymbolDef As SketchedSymbolDefinition
       Try
	   oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item(blockname)
	   Catch
	   Exit Sub
	   End Try
	   Dim oTG As TransientGeometry
       oTG = ThisApplication.TransientGeometry
	   oSketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oTG.CreatePoint2d(17.5, 6.2))
	   oSketchedSymbol.Name = "FirstSketchSymbol"
	   repositionsymbol(oDrawDoc)
	End Sub
	
	Sub repositionsymbol(oDrawDoc As DrawingDocument)
	   Dim oSheet As Sheet
       oSheet = oDrawDoc.ActiveSheet
	   Dim SketchedSymbol As SketchedSymbol
	   Try
		   SketchedSymbol = oSheet.SketchedSymbols("FirstSketchSymbol")
	   Catch
		   Exit Sub
	   End Try
	   Dim oTG As TransientGeometry
       oTG = ThisApplication.TransientGeometry
	   Select Case oSheet.Size
	       Case DrawingSheetSizeEnum.kA0DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(23.5, 6.2)
		   Case DrawingSheetSizeEnum.kA1DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(20.5, 6.2)
		   Case DrawingSheetSizeEnum.kA2DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(17.5, 6.2)
		   Case DrawingSheetSizeEnum.kA3DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(15.5, 6.2)
		   Case DrawingSheetSizeEnum.kA4DrawingSheetSize
			   SketchedSymbol.Position = oTG.CreatePoint2d(11.3, 6.2)
	   End Select
	End Sub
End Class
0 Likes
Message 13 of 16

Miguel.CarvajalS34Q6
Advocate
Advocate

I tried again and it still doesn't work, it can't insert blocks to the drawing sheet

0 Likes
Message 14 of 16

yuzeaa
Advocate
Advocate

have you restarted inventor before running the rule?   I need to leave now and attend the forum tomorrow

Message 15 of 16

Miguel.CarvajalS34Q6
Advocate
Advocate

ok thanks

0 Likes
Message 16 of 16

Miguel.CarvajalS34Q6
Advocate
Advocate

This code works well for me, I just can't get the value of the "item" ("general note ±1") to be a dynamic value that comes from the custom property iProperties.Value("Custom", "RUGOSIDAD") = RUGOSIDAD

 

and I also can't get the coordinate to insert the block to adjust according to the format in use

codigo 1212.jpg

0 Likes