It is possible to obtain a custom variable to use in an array where the variable must be fixed

It is possible to obtain a custom variable to use in an array where the variable must be fixed

Miguel.CarvajalS34Q6
Advocate Advocate
1,379 Views
14 Replies
Message 1 of 15

It is possible to obtain a custom variable to use in an array where the variable must be fixed

Miguel.CarvajalS34Q6
Advocate
Advocate

hello everyone

For several days I have been trying to develop a code that helps me recognize a custom property of the piece and that when inserted in a drawing, the code inserts a block from the sketch symbols folder.
I have a code that allows me to insert the block correctly, but it does not allow me to change it from the part, which is where I have the block selection list in a form.

It is possible to adjust this code so that it recognizes the property and make it dynamic and change according to need.
In addition, it is necessary that the coordinates where the block is inserted change according to the label of the sheet in use. 

 

InventorVb.DocumentUpdate()
iProperties.Value("Custom", "RUGOSIDAD") = RUGOSIDAD

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet


Dim oSketchedSymbolDef As SketchedSymbolDefinition
oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("nota general ±1")



Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry


Dim oSketchedSymbol As SketchedSymbol
oSketchedSymbol = oSheet.SketchedSymbols.Add _
	(oSketchedSymbolDef, oTG.CreatePoint2d(11.3, 6.2), 0, 1, sPromptStrings)

codigo 1212.jpg

 I apologize for the insistent nature of my request, I appreciate the help

 

0 Likes
Accepted solutions (3)
1,380 Views
14 Replies
Replies (14)
Message 2 of 15

WCrihfield
Mentor
Mentor

Hi @Miguel.CarvajalS34Q6.  What you are attempting to describe is somewhat confusing to picture in my mind, and therefore difficult to plan code for how to do it in my mind.  Maybe if you described everything in a bit more detail, it would make more sense.  It sounds like you want to control a SketchedSymbol in a drawing from the model that is being referenced by that drawing.  That sort of scenario can be difficult to manage.  It sounds like you have a multi-value parameter within the model document containing values that you want to use to change some aspect of the SketchedSymbol, but I do not yet understand what those values represent, or how you plan on using them.  If a SketchedSymbol already exists in the drawing, I assume you want to change that existing SketchedSymbol somehow by simply choosing a different value in the part...correct?  If so, then the current SketchedSymbol will likely need to be deleted, or moved, or changed in some other way, but it would have to know that the parameter value in the part has changed somehow, which there is no direct connection for.  It sounds like you may need to somehow monitor for when the value of that one specific parameter changes in the part, then react appropriately to that change.  More pictures may help also.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 15

Andrii_Humeniuk
Advisor
Advisor

Hi @Miguel.CarvajalS34Q6 . If I understood correctly your change RUGOSIDAD is the name of the required block. Next, I added a check to see if your sheet has the right block in the right place, if not, then if necessary, the existing block is removed and a new one is installed. 

InventorVb.DocumentUpdate()
iProperties.Value("Custom", "RUGOSIDAD") = RUGOSIDAD

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet

Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry

Dim oPoint As Point2d = oTG.CreatePoint2d(11.3, 6.2)

For Each oSymb As SketchedSymbol In oSheet.SketchedSymbols
	If oSymb.Position.X = oPoint.X And oSymb.Position.Y = oPoint.Y Then
		If oSymb.Name <> RUGOSIDAD Then
			oSymb.Delete
		Else
			Exit Sub
		End If
	End If
Next

Dim oSketchedSymbolDef As SketchedSymbolDefinition
oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions(RUGOSIDAD)
Dim oSketchedSymbol As SketchedSymbol
oSketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oPoint, 0, 1, sPromptStrings)

 

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 4 of 15

Miguel.CarvajalS34Q6
Advocate
Advocate

ok, thanks for your attention, maybe these images can make what I want to do easier.

0 Likes
Message 5 of 15

Miguel.CarvajalS34Q6
Advocate
Advocate

I am sending this sequence of images to try to better explain what is sought with the code

Diapositiva1.PNG

Diapositiva2.PNG

Diapositiva3.PNG

Diapositiva4.PNG

Diapositiva5.PNG

 thanks for your attention

0 Likes
Message 6 of 15

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi @Miguel.CarvajalS34Q6 . This rule gets your RUGOSIDAD list from the document in the drawing. Next, the rule checks all SketchSymbols on your sheet and if there is a match by name from the list, it gets the position of the symbol and deletes it. If there were no matches on the sheet, the position is chosen according to yours (11.3, 6.2) and the desired symbol is placed. I hope you use only one symbol from the list on the sheet, because if you use several at the same time, the code must be changed. I hope this is what you need.

Sub main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDoc As Document = ThisDoc.Document
	Dim oTG As TransientGeometry = oInvApp.TransientGeometry
	If Not TypeOf oDoc Is DrawingDocument Then Exit Sub
	Dim oDDoc As DrawingDocument = oDoc
	Dim oSheet As Sheet = oDDoc.ActiveSheet
	If oSheet.DrawingViews.Count = 0 Then Exit Sub
	Dim oView As DrawingView = oSheet.DrawingViews(1)
	oDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
	Dim oUsParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
	Dim oRugosidad As UserParameter
	Try : oRugosidad = oUsParams("RUGOSIDAD") : Catch : Exit Sub : End Try
	Dim oListRug() As String = oRugosidad.ExpressionList.GetExpressionList()
	Dim oCustom As PropertySet = oDDoc.PropertySets("Inventor User Defined Properties")
	Try : oCustom("RUGOSIDAD").Value = oRugosidad.Value
	Catch : oCustom.Add(oRugosidad.Value, "RUGOSIDAD") : End Try
	Dim oPoint As Point2d
	Dim oSymbol As SketchedSymbol
	For Each oSymbol In oSheet.SketchedSymbols
		If oSymbol.Name = oRugosidad.Value Then Exit Sub
		For i As Integer = 0 To oListRug.Count - 1
			If oListRug(i).Contains(oSymbol.Name) Then
				oPoint = oSymbol.Position
				oSymbol.Delete()
				Exit For
			End If
		Next i	
	Next
	If oPoint Is Nothing Then oPoint = oTG.CreatePoint2d(11.3, 6.2)
	Dim oSymbolDef As SketchedSymbolDefinition = oDDoc.SketchedSymbolDefinitions(oRugosidad.Value)
	oSymbol = oSheet.SketchedSymbols.Add(oSymbolDef, oPoint, 0, 1, sPromptStrings)
End Sub

 

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 7 of 15

Miguel.CarvajalS34Q6
Advocate
Advocate

The code works perfectly, thank you very much. It is possible to adjust it so that the coordinate where the block is inserted is adjusted according to the format of the active sheet. oTG.CreatePoint2d(11.3, 6.2) for A4 oTG.CreatePoint2d(15.5, 6.2) for A3 oTG.CreatePoint2d(19.4, 6.2) for A2 oTG.CreatePoint2d(23.4, 6.2) for A1 oTG.CreatePoint2d(31.5, 6.2) for A0

0 Likes
Message 8 of 15

Andrii_Humeniuk
Advisor
Advisor

Glad to hear it's working fine, I've added the sheet information to the code, if you don't need to place the new block in the same position as the previous one, you need to comment out line 24. Hope that's what you needed.

Sub main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDoc As Document = ThisDoc.Document
	Dim oTG As TransientGeometry = oInvApp.TransientGeometry
	If Not TypeOf oDoc Is DrawingDocument Then Exit Sub
	Dim oDDoc As DrawingDocument = oDoc
	Dim oSheet As Sheet = oDDoc.ActiveSheet
	If oSheet.DrawingViews.Count = 0 Then Exit Sub
	Dim oView As DrawingView = oSheet.DrawingViews(1)
	oDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
	Dim oUsParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
	Dim oRugosidad As UserParameter
	Try : oRugosidad = oUsParams("RUGOSIDAD") : Catch : Exit Sub : End Try
	Dim oListRug() As String = oRugosidad.ExpressionList.GetExpressionList()
	Dim oCustom As PropertySet = oDDoc.PropertySets("Inventor User Defined Properties")
	Try : oCustom("RUGOSIDAD").Value = oRugosidad.Value
	Catch : oCustom.Add(oRugosidad.Value, "RUGOSIDAD") : End Try
	Dim oPoint As Point2d
	Dim oSymbol As SketchedSymbol
	For Each oSymbol In oSheet.SketchedSymbols
		If oSymbol.Name = oRugosidad.Value Then Exit Sub
		For i As Integer = 0 To oListRug.Count - 1
			If oListRug(i).Contains(oSymbol.Name) Then
				oPoint = oSymbol.Position
				oSymbol.Delete()
				Exit For
			End If
		Next i	
	Next
	If oPoint Is Nothing Then
		Select Case oSheet.Size
		Case DrawingSheetSizeEnum.kA0DrawingSheetSize : oPoint = oTG.CreatePoint2d(31.5, 6.2)
		Case DrawingSheetSizeEnum.kA1DrawingSheetSize : oPoint = oTG.CreatePoint2d(23.4, 6.2)
		Case DrawingSheetSizeEnum.kA2DrawingSheetSize : oPoint = oTG.CreatePoint2d(19.4, 6.2)
		Case DrawingSheetSizeEnum.kA3DrawingSheetSize : oPoint = oTG.CreatePoint2d(15.5, 6.2)
		Case DrawingSheetSizeEnum.kA4DrawingSheetSize : oPoint = oTG.CreatePoint2d(11.3, 6.2)
		End Select
	End If
	Dim oSymbolDef As SketchedSymbolDefinition = oDDoc.SketchedSymbolDefinitions(oRugosidad.Value)
	oSymbol = oSheet.SketchedSymbols.Add(oSymbolDef, oPoint, 0, 1, sPromptStrings)
End Sub

 

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 9 of 15

Miguel.CarvajalS34Q6
Advocate
Advocate

something happens on line 40; It alerts me that the parameter is not correct, the truth is I don't understand this arrangement very much, that's why I haven't been able to intervene.

 

MiguelCarvajalS34Q6_0-1703158620828.png

 

MiguelCarvajalS34Q6_1-1703158669211.png

 

0 Likes
Message 10 of 15

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

I have done the tests and I assure you, the code works perfectly. I suspect the problem is that you are using non-standard sheet formats and lines 32-36 cannot define the sheet format. I've added line 37 which will notify you of an error if the active sheet's format is not standard.

Sub main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDoc As Document = ThisDoc.Document
	Dim oTG As TransientGeometry = oInvApp.TransientGeometry
	If Not TypeOf oDoc Is DrawingDocument Then Exit Sub
	Dim oDDoc As DrawingDocument = oDoc
	Dim oSheet As Sheet = oDDoc.ActiveSheet
	If oSheet.DrawingViews.Count = 0 Then Exit Sub
	Dim oView As DrawingView = oSheet.DrawingViews(1)
	oDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
	Dim oUsParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
	Dim oRugosidad As UserParameter
	Try : oRugosidad = oUsParams("RUGOSIDAD") : Catch : Exit Sub : End Try
	Dim oListRug() As String = oRugosidad.ExpressionList.GetExpressionList()
	Dim oCustom As PropertySet = oDDoc.PropertySets("Inventor User Defined Properties")
	Try : oCustom("RUGOSIDAD").Value = oRugosidad.Value
	Catch : oCustom.Add(oRugosidad.Value, "RUGOSIDAD") : End Try
	Dim oPoint As Point2d
	Dim oSymbol As SketchedSymbol
	For Each oSymbol In oSheet.SketchedSymbols
		If oSymbol.Name = oRugosidad.Value Then Exit Sub
		For i As Integer = 0 To oListRug.Count - 1
			If oListRug(i).Contains(oSymbol.Name) Then
				oPoint = oSymbol.Position
				oSymbol.Delete()
				Exit For
			End If
		Next i	
	Next
	If oPoint Is Nothing Then
		Select Case oSheet.Size
		Case DrawingSheetSizeEnum.kA0DrawingSheetSize : oPoint = oTG.CreatePoint2d(31.5, 6.2)
		Case DrawingSheetSizeEnum.kA1DrawingSheetSize : oPoint = oTG.CreatePoint2d(23.4, 6.2)
		Case DrawingSheetSizeEnum.kA2DrawingSheetSize : oPoint = oTG.CreatePoint2d(19.4, 6.2)
		Case DrawingSheetSizeEnum.kA3DrawingSheetSize : oPoint = oTG.CreatePoint2d(15.5, 6.2)
		Case DrawingSheetSizeEnum.kA4DrawingSheetSize : oPoint = oTG.CreatePoint2d(11.3, 6.2)
		Case Else : MessageBox.Show("Your sheet don't standart.", "Error!") : Exit Sub
		End Select
	End If
	Dim oSymbolDef As SketchedSymbolDefinition = oDDoc.SketchedSymbolDefinitions(oRugosidad.Value)
	oSymbol = oSheet.SketchedSymbols.Add(oSymbolDef, oPoint, 0, 1, sPromptStrings)
End Sub

 In case my suspicion is correct, you need to let me know the sizes of your formats and I will add a size selector.

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 11 of 15

Miguel.CarvajalS34Q6
Advocate
Advocate

OK, it is true that I have different sizes than the standard ones. I have a code that allows me to adjust the sizes for quick selection of formats.

 

MiguelCarvajalS34Q6_0-1703173933726.png

That detail will be what causes the problem when running the code.

0 Likes
Message 12 of 15

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Unfortunately on your screenshot I see information about A4 format, so I made a selector by the name of the border ("formato_a4") lines 32-36:

 

Sub main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDoc As Document = ThisDoc.Document
	Dim oTG As TransientGeometry = oInvApp.TransientGeometry
	If Not TypeOf oDoc Is DrawingDocument Then Exit Sub
	Dim oDDoc As DrawingDocument = oDoc
	Dim oSheet As Sheet = oDDoc.ActiveSheet
	If oSheet.DrawingViews.Count = 0 Then Exit Sub
	Dim oView As DrawingView = oSheet.DrawingViews(1)
	oDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
	Dim oUsParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
	Dim oRugosidad As UserParameter
	Try : oRugosidad = oUsParams("RUGOSIDAD") : Catch : Exit Sub : End Try
	Dim oListRug() As String = oRugosidad.ExpressionList.GetExpressionList()
	Dim oCustom As PropertySet = oDDoc.PropertySets("Inventor User Defined Properties")
	Try : oCustom("RUGOSIDAD").Value = oRugosidad.Value
	Catch : oCustom.Add(oRugosidad.Value, "RUGOSIDAD") : End Try
	Dim oPoint As Point2d
	Dim oSymbol As SketchedSymbol
	For Each oSymbol In oSheet.SketchedSymbols
		If oSymbol.Name = oRugosidad.Value Then Exit Sub
		For i As Integer = 0 To oListRug.Count - 1
			If oListRug(i).Contains(oSymbol.Name) Then
				oPoint = oSymbol.Position
				oSymbol.Delete()
				Exit For
			End If
		Next i	
	Next
	If oPoint Is Nothing Then		
		Select Case oSheet.Border.Name
		Case "FORMATO_A0" : oPoint = oTG.CreatePoint2d(31.5, 6.2)
		Case "FORMATO_A1" : oPoint = oTG.CreatePoint2d(23.4, 6.2)
		Case "FORMATO_A2" : oPoint = oTG.CreatePoint2d(19.4, 6.2)
		Case "FORMATO_A3" : oPoint = oTG.CreatePoint2d(15.5, 6.2)
		Case "FORMATO_A4" : oPoint = oTG.CreatePoint2d(11.3, 6.2)
		Case Else : MessageBox.Show("Your sheet don't standart.", "Error!") : Exit Sub
		End Select
	End If
	Dim oSymbolDef As SketchedSymbolDefinition = oDDoc.SketchedSymbolDefinitions(oRugosidad.Value)
	oSymbol = oSheet.SketchedSymbols.Add(oSymbolDef, oPoint, 0, 1, sPromptStrings)
End Sub

 

You can also make the sheet selector yourself (by height) :

 


		Select Case oSheet.Height
		Case 000 : oPoint = oTG.CreatePoint2d(31.5, 6.2)
		Case 000 : oPoint = oTG.CreatePoint2d(23.4, 6.2)
		Case 000 : oPoint = oTG.CreatePoint2d(19.4, 6.2)
		Case 000 : oPoint = oTG.CreatePoint2d(15.5, 6.2)
		Case 307 : oPoint = oTG.CreatePoint2d(11.3, 6.2)
		Case Else : MessageBox.Show("Your sheet don't standart.", "Error!") : Exit Sub
		End Select

 

by width:

 


		Select Case oSheet.Width
		Case 000 : oPoint = oTG.CreatePoint2d(31.5, 6.2)
		Case 000 : oPoint = oTG.CreatePoint2d(23.4, 6.2)
		Case 000 : oPoint = oTG.CreatePoint2d(19.4, 6.2)
		Case 000 : oPoint = oTG.CreatePoint2d(15.5, 6.2)
		Case 225 : oPoint = oTG.CreatePoint2d(11.3, 6.2)
		Case Else : MessageBox.Show("Your sheet don't standart.", "Error!") : Exit Sub
		End Select

 

Choose what you like more, but make sure that the data in the cases are true.

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 13 of 15

Miguel.CarvajalS34Q6
Advocate
Advocate

The code provides a very good solution, but I think that working with custom formats generates problems, it does not work because the code recognizes standard formats. For now I will work manually until I can solve the problems with the formats

0 Likes
Message 14 of 15

Miguel.CarvajalS34Q6
Advocate
Advocate

I attach the files I work with, so you can see how I am working

Message 15 of 15

Andrii_Humeniuk
Advisor
Advisor

Hello @Miguel.CarvajalS34Q6 . I noticed that your borders have a name without a bottom line (example "FORMATO A4"). So I made changes to the border names and the code works perfectly for your situation.

I also suppressed line 24, it doesn't seem necessary for you.

Sub main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDoc As Document = ThisDoc.Document
	Dim oTG As TransientGeometry = oInvApp.TransientGeometry
	If Not TypeOf oDoc Is DrawingDocument Then Exit Sub
	Dim oDDoc As DrawingDocument = oDoc
	Dim oSheet As Sheet = oDDoc.ActiveSheet
	If oSheet.DrawingViews.Count = 0 Then Exit Sub
	Dim oView As DrawingView = oSheet.DrawingViews(1)
	oDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
	Dim oUsParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
	Dim oRugosidad As UserParameter
	Try : oRugosidad = oUsParams("RUGOSIDAD") : Catch : Exit Sub : End Try
	Dim oListRug() As String = oRugosidad.ExpressionList.GetExpressionList()
	Dim oCustom As PropertySet = oDDoc.PropertySets("Inventor User Defined Properties")
	Try : oCustom("RUGOSIDAD").Value = oRugosidad.Value
	Catch : oCustom.Add(oRugosidad.Value, "RUGOSIDAD") : End Try
	Dim oPoint As Point2d
	Dim oSymbol As SketchedSymbol
	For Each oSymbol In oSheet.SketchedSymbols
		If oSymbol.Name = oRugosidad.Value Then Exit Sub
		For i As Integer = 0 To oListRug.Count - 1
			If oListRug(i).Contains(oSymbol.Name) Then
				oPoint = oSymbol.Position
				oSymbol.Delete()
				Exit For
			End If
		Next i	
	Next
	If oPoint Is Nothing Then		
		Select Case oSheet.Border.Name
		Case "FORMATO A0" : oPoint = oTG.CreatePoint2d(31.5, 6.2)
		Case "FORMATO A1" : oPoint = oTG.CreatePoint2d(23.4, 6.2)
		Case "FORMATO A2" : oPoint = oTG.CreatePoint2d(19.4, 6.2)
		Case "FORMATO A3" : oPoint = oTG.CreatePoint2d(15.5, 6.2)
		Case "FORMATO A4" : oPoint = oTG.CreatePoint2d(11.3, 6.2)
		Case Else : MessageBox.Show("Your sheet don't standart.", "Error!") : Exit Sub
		End Select
	End If
	Dim oSymbolDef As SketchedSymbolDefinition = oDDoc.SketchedSymbolDefinitions(oRugosidad.Value)
	oSymbol = oSheet.SketchedSymbols.Add(oSymbolDef, oPoint, 0, 1, sPromptStrings)
End Sub

 Make sure your borders are spelled correctly in lines 32-36 and you'll be fine 🙂

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