ilogic code for view labelling

ilogic code for view labelling

rankinkane
Contributor Contributor
1,834 Views
6 Replies
Message 1 of 7

ilogic code for view labelling

rankinkane
Contributor
Contributor

Hi,

I am very new to coding and I would like to know if there is a way to use code to add set values to a drawing view. Mainly for use with flat patterns but I would also like to use it for standard parts and just drop the thickness value.

I would like to add these parameters;

Item number from parts list,

part number,

sheet metal thickness,

and material.

Like this

rankinkane_0-1639103068595.png

 

If anyone could help me out that would be great.

Thanks in advance

0 Likes
Accepted solutions (1)
1,835 Views
6 Replies
Replies (6)
Message 2 of 7

A.Acheson
Mentor
Mentor

Here is a few links, you will need to work with the view.label.formattedtext. The view.name is the name given to the view in dialogue used for setting up the view.  You can either just add the property values as text or add link properties (much harder ) that will update when the model changes. You can already add a lot of these all ready manually in the drawing templates. 

 

Here is a simple one to get you started

http://forums.autodesk.com/t5/inventor-customization/ilogic-code-to-change-view-label-text/td-p/3625...

 

Dim oSSet As SelectSet = ThisDoc.Document.SelectSet
If oSSet.count = 0 Then
	MessageBox.Show("You must select a drawing view first", "iLogic")
Exit Sub
End If

'Reference to the drawing view from the 1st selected object
Dim oView As DrawingView = trycast(oSSet.item(1), DrawingView)

If oView IsNot Nothing Then
oView.ShowLabel = True
'format the model iproperties	
oDescription = "<StyleOverride Underline='True'><Property Document='model' PropertySet='Design Tracking Properties' Property='Description' FormatID='{32853F0F-3444-11D1-9E93-0060B03C1CA6}' PropertyID='29'>DESCRIPTION</Property></StyleOverride>"
oPartNumber = "<StyleOverride Underline='True'> - Mk <Property Document='model' PropertySet='Design Tracking Properties' Property='Part Number' FormatID='{32853F0F-3444-11D1-9E93-0060B03C1CA6}' PropertyID='5'>PART NUMBER</Property></StyleOverride>"
oStringMass = "<Br/><StyleOverride Underline='False' FontSize='0.35'>EST UNIT MASS = <PhysicalProperty PhysicalPropertyID='72449' Precision='2'>MASS</PhysicalProperty></StyleOverride>"
oStringScale = "<Br/><StyleOverride FontSize='0.3'>(Scale <DrawingViewScale/>)</StyleOverride>"

'add to the view label
oView.Label.FormattedText =  oDescription & oPartNumber & oStringMass & oStringScale	    

Else
	MessageBox.Show("The selected object is not a drawing view", "iLogic")
End If

 This one shows more detail on getting custom attributes into the view label. 

http://forums.autodesk.com/t5/inventor-customization/custom-user-properties-in-view-label-of-drawing...

 

And this one will work with assemblies. 

http://blog.ads-sol.com/2016/02/item-number-on-drawing-views-reg-hasell.html

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 3 of 7

J-Camper
Advisor
Advisor

@rankinkane,

 

The only part of you request that requires further information is how to find the correct assembly to get item number for the part.  If you can help me understand how the part/drawing/assembly are associated we can find the Item number.  For the rest of the code see the following:

'Anything Outside The 'main' routine only runs when called
Sub Main
	'Try to set Active Document to a DrawingDocument Object
	Dim dDoc As DrawingDocument = TryCast(ThisApplication.ActiveDocument, DrawingDocument) 'This Fails to Nothing
	If IsNothing(dDoc) Then Logger.Debug("Not A DrawingDocuemnt") : Exit Sub
	'We know we have a drawing document open
	'Get User to select a view
	Dim PickView As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select a View")
	If IsNothing(PickView) Then Exit Sub 'If nothing gets selected then we're done
	'We now have a drawing view
	'Call routine for the selected view.
	Call ViewInfo(PickView)
	'Breaking up code like this helps for multipurposing.
	'If you want you can loop through all the views on a sheet
End Sub

'Sub Routine: Processes Custom Options for a given DrawingView Object
Sub ViewInfo(dView As DrawingView)
	'Define some Objects
	Dim ModelDoc As Document = dView.ReferencedDocumentDescriptor.ReferencedDocument
	Dim ItemNumber As String 'I will need more information to make this work
	Dim mPartNumber As String 'ModelDocument PartNumber
	Dim smThickness As String 'SheetMetal Thickness As a number 
	Dim mMaterial As String 'ModelDocument Material

	'Try to set the model document to a PartDocument Object
	Dim pDoc As PartDocument = TryCast(ModelDoc, PartDocument)
	If Not IsNothing(pDoc) 'As long as it is anything/not nothing
		ItemNumber = FindAssemblyItemNumber(pDoc) 'Call Custom Function to get Item Number
		mPartNumber = pDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value 'Get PartNumber iProperty
		smThickness = GetSheetMetalThickness(pDoc) 'Call Custom Function to get Sheet Metal Thickness [I'm going to return mm]
		mMaterial = pDoc.ActiveMaterial.DisplayName
		'Now I am sending the gathered information to the drawingview label.
		'If you want to put it somehwere else, let me know
		Call SetLabel(dView, ItemNumber, mPartNumber, smThickness, mMaterial) 'Custom Sub Routine
	End If
	
End Sub

'Sub Routine: Sets the DrawingView Label to a custom string format, centered & bottom of view for a given DrawingView Object
Sub SetLabel(dView As DrawingView, s1 As String, s2 As String, s3 As String, s4 As String)
	Dim ThisSheet As Sheet = dView.Parent 'Get The Sheet the view is on
	'Set Text to dsiplay
	Dim FormattedText As String = "<StyleOverride Underline='True'>" & s1 & " - " & s2 & "</StyleOverride><Br/>" & s3 & s4 
	'SetThe DrawingView Label & make sure it is visible & @ Bottom Center
	dView.ShowLabel = True
	Dim dvLabel As DrawingViewLabel = dView.Label
	dvLabel.FormattedText = FormattedText
	dvLabel.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter
	dvLabel.VerticalJustification = VerticalTextAlignmentEnum.kAlignTextLower
End Sub

'A Function sends something back after being called. It gets defined like any other Object
'Function: Return {AS String} Item number of given part document in Assembly. {assembly currently unknown}
Function FindAssemblyItemNumber(pDoc As PartDocument) As String
	Dim Result As String = "Need Assembly Info" 'Defualt Result
	
	'I need more information to find the correct assmebly from a partdocument drawing view
	
	Return Result
End Function

'Function: Return {AS String} SheetMetal Thickness for given PartDocument Object
Function GetSheetMetalThickness(pDoc As PartDocument) As String
	Dim Result As String = "" 'Defualt Result
	Dim ResultValue As Double 'Set Object For Double Handling
	
	'Try to set a SheetMetalComponentDefinition Object
	Dim smDef As SheetMetalComponentDefinition = TryCast(pDoc.ComponentDefinition, SheetMetalComponentDefinition)
	If IsNothing(smDef) Then Return Result 'If The PartDocument is not a sheetmetal part return our 'nothing' default
		
	'We know we have a view of a sheetmetal Part and can call Specific Properties and methods
	ResultValue = smDef.Thickness.Value 'Database units for Length is cm. We have to convert to our desired unit type
	ResultValue *= 10 'I'm converting the value from cm to mm by multiplying itself by 10
	'I'm going to return mm with 3 decimals
	Result = Math.Round(ResultValue, 3).ToString & " mm "
	
	Return Result
End Function

After running the rule you will be asked to select a DrawingView  [this could easily be adapted to run for every DrawingView on a given sheet, or even every DrawingView on every sheets].  I wrote it in a modular structure, as it allows for easier adaptation to various processes.  The label is structured as shown in the image posted.

 

Let me know if you have any questions, as you stated you are new to coding.  Also let me know how to relate an assembly to a particular part in a particular drawing.

Message 4 of 7

fidel.makatiaD5W7V
Alumni
Alumni

Hi @rankinkane kindly go through these quick tutorials for ilogic with inventor. Let me know in case of any clarification

For drawing views

Inventor 2022 Help | Drawing Views | Autodesk

For dimensions

Inventor 2022 Help | Drawing Dimensions | Autodesk

For adding Balloons

Inventor 2022 Help | Balloons | Autodesk

For adding custom tables 

Inventor 2022 Help | Custom Tables | Autodesk

 

 



Fidel Makatia
Developer Advocate

href=https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=GUID-0BD48573-7193-4285-87B7-6727555D053E rel= "noopener noreferrer">Inventor 2022 Documentation |
Message 5 of 7

rankinkane
Contributor
Contributor

Thanks very much for your time. 

 

I probably should have lead the enquiry with this but I have some code for extracting the item number and scale for the views. I would like to replace what I have with the information provided on the original post.

 

Code I have so far.

 

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oSheets As Sheets
Dim Sheet As Inventor.Sheet
Dim oViews As DrawingViews
Dim oView As DrawingView

For Each oSheet In oDrawDoc.Sheets
'For Each oSheet In oSheets
    oViews = oSheet.DrawingViews
    For Each oView In oViews
   
        'Get the full filename Of the view model
        Dim oModelFileName As String
        oModelFileName = oView.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName
        'MessageBox.Show("view model name" & oModelFileName, "Title")

        Dim oPartList As PartsList
            'try and get the parts list form the table of this sheet
            Try
                oPartList = oDrawDoc.ActiveSheet.PartsLists.Item(1)
            Catch 'on error try and search all sheets for first found parts list           
                'iterate trough each sheet
                Dim i As Long
                For i = 1 To oDrawDoc.Sheets.Count
                    If oDrawDoc.Sheets.Item(i).PartsLists.Count > 0 Then Exit For
                Next   
                oPartList = oDrawDoc.Sheets.Item(i).PartsLists.Item(1)
                'MessageBox.Show("parts list found on: " & i, "Title")
            End Try
               
            ' Iterate through the contents of the parts list.
            Dim j As Long
            For j = 1 To oPartList.PartsListRows.Count
                ' Get the current row.
                Dim oRow As PartsListRow
                oRow = oPartList.PartsListRows.Item(j)
                'get filename of model in row
                Dim oRowFileName As String
                oRowFileName = oRow.ReferencedFiles.Item(1).FullFileName
                'compare the filenames
                'Performs a text comparison, based on a case-insensitive text sort order
                'If strings equal returns 0
                If StrComp(oModelFileName, oRowFileName, CompareMethod.Text)=0 Then
                    'Get the value of Item from the Parts List
                    'Row name needs to be case sensitive or use 1 for first 2 for second etc.
                    oCell  = oPartList.PartsListRows.Item(j).Item("Item") 'Row name needs to be case sensitive or use 1 for first 2 for second etc.
                    'get the value of text in cell
                    Dim oItemValue As String
                    oItemValue = oCell.Value
                   
                    'Show the view label
                    oView.ShowLabel = True
                    'format the text first line
                    oStringItem = "<StyleOverride Underline='True' FontSize='0.35'> ITEM " & oItemValue & " </StyleOverride>"
                    'format the text second line
                    oStringScale = "<Br/><StyleOverride FontSize='0.3'>(Scale <DrawingViewScale/>)</StyleOverride>"
                   
                    'add to the view label
                    oView.Label.FormattedText =  oStringItem & oStringScale
                End If 
            Next
      Next
Next

 

0 Likes
Message 6 of 7

J-Camper
Advisor
Advisor
Accepted solution

I added your PartList/Item number portion of the posted code to the "FindAssemblyItemNumber" Function of my original post.  See complete code below:

'Anything Outside The 'main' routine only runs when called
Sub Main
	'Try to set Active Document to a DrawingDocument Object
	Dim dDoc As DrawingDocument = TryCast(ThisApplication.ActiveDocument, DrawingDocument) 'This Fails to Nothing
	If IsNothing(dDoc) Then Logger.Debug("Not A DrawingDocuemnt") : Exit Sub
	'We know we have a drawing document open
	'Get User to select a view
	Dim PickView As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select a View")
	If IsNothing(PickView) Then Exit Sub 'If nothing gets selected then we're done
	'We now have a drawing view
	'Call routine for the selected view.
	Call ViewInfo(PickView)
	'Breaking up code like this helps for multipurposing.
	'If you want you can loop through all the views on a sheet
End Sub

'Sub Routine: Processes Custom Options for a given DrawingView Object
Sub ViewInfo(dView As DrawingView)
	'Define some Objects
	Dim ModelDoc As Document = dView.ReferencedDocumentDescriptor.ReferencedDocument
	Dim ItemNumber As String 'I will need more information to make this work
	Dim mPartNumber As String 'ModelDocument PartNumber
	Dim smThickness As String 'SheetMetal Thickness As a number 
	Dim mMaterial As String 'ModelDocument Material

	'Try to set the model document to a PartDocument Object
	Dim pDoc As PartDocument = TryCast(ModelDoc, PartDocument)
	If Not IsNothing(pDoc) 'As long as it is anything/not nothing
		ItemNumber = FindAssemblyItemNumber(pDoc, dView.Parent.Parent) 'Call Custom Function to get Item Number
		mPartNumber = pDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value 'Get PartNumber iProperty
		smThickness = GetSheetMetalThickness(pDoc) 'Call Custom Function to get Sheet Metal Thickness [I'm going to return mm]
		mMaterial = pDoc.ActiveMaterial.DisplayName
		'Now I am sending the gathered information to the drawingview label.
		'If you want to put it somehwere else, let me know
		Call SetLabel(dView, ItemNumber, mPartNumber, smThickness, mMaterial) 'Custom Sub Routine
	End If
	
End Sub

'Sub Routine: Sets the DrawingView Label to a custom string format, centered & bottom of view for a given DrawingView Object
Sub SetLabel(dView As DrawingView, s1 As String, s2 As String, s3 As String, s4 As String)
	Dim ThisSheet As Sheet = dView.Parent 'Get The Sheet the view is on
	'Set Text to dsiplay
	Dim FormattedText As String = "<StyleOverride Underline='True'>" & s1 & " - " & s2 & "</StyleOverride><Br/>" & s3 & s4 
	'SetThe DrawingView Label & make sure it is visible & @ Bottom Center
	dView.ShowLabel = True
	Dim dvLabel As DrawingViewLabel = dView.Label
	dvLabel.FormattedText = FormattedText
	dvLabel.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter
	dvLabel.VerticalJustification = VerticalTextAlignmentEnum.kAlignTextLower
End Sub

'A Function sends something back after being called. It gets defined like any other Object
'Function: Return {AS String} Item number of given part document in Assembly. {assembly currently unknown}
Function FindAssemblyItemNumber(pDoc As PartDocument, oDrawDoc As DrawingDocument) As String
	Dim Result As String = "Assembly Info Not Found" 'Defualt Result
	
	Dim oPartList As PartsList
    'try and get the parts list form the table of this sheet
    Try
        oPartList = oDrawDoc.ActiveSheet.PartsLists.Item(1)
    Catch 'on error try and search all sheets for first found parts list           
        'iterate trough each sheet
        Dim i As Long
        For i = 1 To oDrawDoc.Sheets.Count
            If oDrawDoc.Sheets.Item(i).PartsLists.Count > 0 
				oPartList = oDrawDoc.Sheets.Item(i).PartsLists.Item(1)
				Exit For
			End If
        Next   
        'MessageBox.Show("parts list found on: " & i, "Title")
    End Try
      
	If IsNothing(oPartList) Then Logger.Debug("No PartsList found in drawing document") : Return Result
	
    ' Iterate through the contents of the parts list.
    Dim j As Long
    For j = 1 To oPartList.PartsListRows.Count
        ' Get the current row.
        Dim oRow As PartsListRow = oPartList.PartsListRows.Item(j)
        'get filename of model in row
        Dim oRowFileName As String = oRow.ReferencedFiles.Item(1).FullFileName
        'compare the filenames
        'Performs a text comparison, based on a case-insensitive text sort order
        'If strings equal returns 0
        If StrComp(pDoc.FullFileName, oRowFileName, CompareMethod.Text)=0
            'Get the value of Item from the Parts List
            'Row name needs to be case sensitive or use 1 for first 2 for second etc.
            oCell  = oPartList.PartsListRows.Item(j).Item("Item") 'Row name needs to be case sensitive or use 1 for first 2 for second etc.
            'get the value of text in cell
            Result = oCell.Value
			Return Result
		End If
	Next
	Return Result
End Function

'Function: Return {AS String} SheetMetal Thickness for given PartDocument Object
Function GetSheetMetalThickness(pDoc As PartDocument) As String
	Dim Result As String = "" 'Defualt Result
	Dim ResultValue As Double 'Set Object For Double Handling
	
	'Try to set a SheetMetalComponentDefinition Object
	Dim smDef As SheetMetalComponentDefinition = TryCast(pDoc.ComponentDefinition, SheetMetalComponentDefinition)
	If IsNothing(smDef) Then Return Result 'If The PartDocument is not a sheetmetal part return our 'nothing' default
		
	'We know we have a view of a sheetmetal Part and can call Specific Properties and methods
	ResultValue = smDef.Thickness.Value 'Database units for Length is cm. We have to convert to our desired unit type
	ResultValue *= 10 'I'm converting the value from cm to mm by multiplying itself by 10
	'I'm going to return mm with 3 decimals
	Result = Math.Round(ResultValue, 3).ToString & " mm "
	
	Return Result
End Function

 I changed the PartsList Try/Catch so it doesn't error out if no PartsList exists in the drawing, but left the rest of the formatting as you wrote it. 

 

Let me know if you have any questions, or if this is not working as intended.

Message 7 of 7

rankinkane
Contributor
Contributor

Thanks very much for that. It works great. 

 

I just need to add The text "ITEM NUMBER" before the item number itself but I will try and work that one out myself.

 

 

0 Likes