Get list of Tube and Pipe Styles using API

Get list of Tube and Pipe Styles using API

waynehelley
Collaborator Collaborator
1,798 Views
8 Replies
Message 1 of 9

Get list of Tube and Pipe Styles using API

waynehelley
Collaborator
Collaborator

Hi all,

 

I have seen a few threads about there being no API for Tube and Pipe so what I want might not be possible.

 

I just want to grab a list of names of all the currently available Tube and Pipe styles.

 

for each style in tubeAndPipeStyles

     myList.Add(style.Name)

Next

 

It would be a bonus to be able to get the Diameter, Thickness and Default Radius.

 

Has anybody tried this?

 

Thanks,

 

Wayne

Wayne Helley
Inventor 2013 Certified Professional

Autodesk Inventor Professional 2023
Visual Studio 2022
Windows 10 Pro, 64-bit
0 Likes
Accepted solutions (4)
1,799 Views
8 Replies
Replies (8)
Message 2 of 9

A.Acheson
Mentor
Mentor

Here is a method to extract style names from tube and pipe styles. Export first the style/category of styles to XML file. Then use 1. T&P Extract Style Names ilogic rule from this post. 

 

https://knowledge.autodesk.com/community/article/345326

 

This part of a larger ilogic rule to

Detect Tube and Pipe Active Route Style and load Route Parameters- iLogic External Rules

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

Tom_Fuller
Contributor
Contributor
Accepted solution

Yes, it is possible to get the Tube & Pipe Styles, without having to export the styles using Inventor. It turns out that each of the styles is stored in the Tube & Pipe Runs document as a PropertySet. Now these aren't actually accessible through the API provided. 

However, All Properties/PropertySets are actually accessible. Inventor files are actually compound files (See Link) and you can access all the OLE PorpertySet data, including the iProperties and T&P styles. you can see the background on this process Here.

Now I initially discovered this using this application: 

Compound File Explorer (Installer Attached)

N.B. this is only a trial for the software

 

When you open the "Tube & Pipe Runs.iam" with this application, you will be able to traverse the PropertySets, which are located at the root of the compound file.

 

There is also a couple GitHub Repos for a class library that can be used to achieve what you are after:
GitHub - provegard/openmcdf-3

NuGet Gallery | OpenMcdf 2.2.1.9

 

This should be a good place to start ☺️ But in the New Year, I will post a full solution. 

0 Likes
Message 4 of 9

A.Acheson
Mentor
Mentor
Accepted solution

It seems the contribution post I mentioned above has been discontinued. Here is the information contained in that post. Note rule 1 below will extract the style names from styles xml file. 

 

Import Parameters to Route Workflow:

When using the Add-in Tube and Pipe, parameters can be difficult to utilize within the route. It can be very time-consuming to search for and load the parameters associated with the piping diameter and style. Run and Route templates are not available to store parameters unlike part and assembly templates. In order to use standardized parameters for each pipe style, it is necessary to import parameters into each route when required. This is a manual process as shown in the below image. 

AAcheson_0-1679711862248.png

 

The four external rules below are a workflow to automate this process. Rules 1-2 prepare the part file for parameter creation; the user then creates the necessary parameters before using rule 3 to export parameters to an xml file. Rule 4 is the main rule to detect the active route style and load the parameters associated to this style.

To ensure filenames have no illegal characters that would prevent a file save and to improve consistency, a function is used in Rules 2&4. Please ensure any changes made to the functions are mirrored in each rule. 

 

1. T&P Extract Style Names

Use this rule to extract current style names to an excel sheet in order to standardize naming conventions. Before running this rule, find the tube and pipe styles tab on the ribbon and access the dialogue box. Select styles by category for example category (Rigid Pipe With Fittings) see image, right click and manually export and save to  "C:\Temp\Tube and Pipe Styles.xml." Adjust this file path as required and change in the code below.

 

AddReference "Microsoft.Office.Interop.Excel.dll" 'To use excel
Imports Xl = Microsoft.Office.Interop.Excel 'To use excel

Sub Main
	
	'Specify the XML path and filename 
	Dim XMLFile As String = InputBox("Prompt", "Styles Path XML File", "C:\Temp\Tube and Pipe Styles.xml")
	'Specify the excel path and filename 
	Dim XLFile As String = InputBox("Prompt", "Styles excel file", "C:\Temp\Tube and Pipe Styles.xlsx")
	
	'Start a new instance of Excel, make visible and disable alerts.
	Dim excelApp As Object = CreateObject("Excel.Application")'XL.Application
	Dim xlwb As XL.Workbook
	Dim xlws As XL.Worksheet
	
	excelApp.Visible = True
	excelApp.DisplayAlerts = False
	
	'Open Excel worksheet and import xml information.
	Try
		xlwb = excelApp.Workbooks.OpenXML(Filename :=XMLFile,LoadOption :=xlXmlLoadImportToList)
	Catch
		MessageBox.Show("Cannot find XML File: Exiting", "iLogic")
		Exit Sub
	End Try
	
	'Specify the excel sheet.
	xlws = xlwb.Worksheets(1)
	xlws.Activate
	
	'Format height of sheet rows.
	xlws.Rows("1:500").RowHeight = 15
	
	'Save the workbook.
	xlwb._SaveAs(XLFile)
	
	excelApp.DisplayAlerts = True
	
	'Close the Workbook.
	'xlwb.Close
End Sub

 

 

2. T&P Create Route Parameter Part File

Rule modified from Source and uses function to remove illegal characters.

 

Sub Main
	
	'Open the excel worksheet containing the style names, modify path as needed .
	 Dim XLFile As String = InputBox("Prompt", "Styles excel file", "C:\Temp\Tube and Pipe Styles.xlsx")
	
	GoExcel.Open(XLFile, "Sheet1")

	'Set the list of Style Names reading from the excel worksheet.
	Dim StyleList As New List (Of String)
	
	Dim i As Integer = GoExcel.FindRowStart 
	
	'Define the number of rows we want to search in
	Do Until i = 100 + GoExcel.FindRowStart  ' 100 rows plus the start row position'
		'Add name to list excluding blank cells
		If GoExcel.CellValue(XLFile, "Sheet1", "A" & i) = ""
			Exit Do
		End If
		StyleList.Add(GoExcel.CellValue(XLFile, "Sheet1", "A" & i)) 
		i = i + 1
	Loop

	'Present the list to the user
	Dim StyleName As String = InputListBox("Select a name", StyleList, "", "iLogic", "Available Style Names")
	'MessageBox.Show(StyleName, "iLogic")

	'Filename creation,replacement string for fileName ilegal Characters.
	Dim replace_with As String = ""

	'Function to remove ilegal characters from Style Name before saving file
	Dim oFileName As String = clean_filename(StyleName, replace_with)
	
	'MessageBox.Show(oFileName, "iLogic")

	'Option to use exisitng part and save as or create a new one
	Question = InputRadioBox("Pick One", "Existing File Save As New Name", "Create New Part File", True, Title := "File Source") 

	Dim oPartDoc As PartDocument
	Dim oFilePath As String
	If Question = True
		' Create a new part file
		oPartDoc = ThisDoc.Document 
		oFilePath = ThisDoc.Path & "\"
	Else
		'note: kPartDocumentObject is the default template,specify template file to use
		oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, , True)'"C:\Temp\Standard.ipt" Modify file as needed.
		oFilePath = "C:\Temp\" 
	End If
	 
		Dim oPartFile As String = oFilePath & oFileName & ".ipt"

		'Check for existing file 
		If System.IO.File.Exists(oPartFile) Then
			
			'Present option for Filecheck to avoid overwriting file accidentally
			FileCheck = InputRadioBox("Pick One", "Overwite Existing File", "Exit without Save", True, Title := "Part File Allready Exists") 

			If FileCheck = True
				Try
					'Overwrite the file if it exist
					oPartDoc.SaveAs(oPartFile, False)
				Catch
					MessageBox.Show("Part File save problem,Using Save As on a file of the same name", "iLogic")
				End Try
			Else 
				Exit Sub
			End If
			
		'Save the file if it does not exist
		ElseIf Not System.IO.File.Exists(oPartFile) Then
			oPartDoc.SaveAs(oPartFile, False)
		End If
	
End Sub

Function clean_filename(StyleName As String, replace_with As String)
    Dim inv_chars As String
    Dim cpos As Long

	'Origional List adjust function contents in invchars as needed.
	'"'!""#¤%&/()=?`^*>;:@£${[]}|~\,.'¨´+-"

    invchars = "'!""#¤%&/=?`^*>;:@£${[]}|~\,.'¨´+"
    For cpos = 1 To Len(invchars)
        StyleName = Replace(StyleName, Mid(invchars, cpos, 1), replace_with)
    Next
    clean_filename = StyleName
End Function

 

 

3. T&P Export Route Parameters

 

'Get active Directory
Dim oDirectory As String = "C:\Temp\"

'Get active filename
Dim oName As String = ThisDoc.FileName(False) 'without extension

'Set new name for xml file
Dim oXMLFile As String = oDirectory & oName & ".xml"

'MessageBox.Show(oXMLFile, "iLogic")

'Check for existing file 
If System.IO.File.Exists(oXMLFile) Then
	
	'Present option for Filecheck to avoid overwriting file accidentally.
	Dim FileCheck As Boolean = InputRadioBox("Pick One", "Overwite Existing File", "Exit without Save", True, Title := "File Allready Exists") 

	If FileCheck = True
		Try
			'Overwrite the file if it exist.
			iLogicVb.Automation.ParametersXmlSave(ThisDoc.Document, oXMLFile)
		Catch
			MessageBox.Show("File save problem", "iLogic")
		End Try
	Else 
		Return
	End If
'Save the file if it does not exist.
ElseIf Not System.IO.File.Exists(oXMLFile) Then
	iLogicVb.Automation.ParametersXmlSave(ThisDoc.Document, oXMLFile)
End If

MessageBox.Show("Parameters exported", "iLogic")

 

 

4. T&P Import Route Parameters

 

Sub Main
		
	Dim oDoc As Document = ThisDoc.Document

	Dim oCDs As ControlDefinitions = ThisApplication.CommandManager.ControlDefinitions 
	
	'Active Style (Combo Box Tube and Pipe Styles)
	Dim oCD As ControlDefinition= oCDs("HSL:Piping:StylesComboBox")

	'Get active style name.
	Dim StyleName As String = oCD.ToolTipText
	'MessageBox.Show(StyleName, "iLogic")

	'File Name Creation Replacement String for FileName ilegal Characters. 
	Dim replace_with As String = ""

	'Function to remove ilegal characters from filename if present, adjust function contents as needed.
	 Dim oFileName As String = clean_filename(StyleName, replace_with)
	'MessageBox.Show(sNewName, "iLogic")

	'Import XML
	If oFileName = "" Then : Return : End If
	
	'File directory where xml file is stored.
	Dim FilePath As String = "C:\Temp\"
	
	'Set new name for xml file.
	Dim oXMLFile As String = FilePath & oFileName & ".xml"
	
	'Open the selected file.
	iLogicVb.Automation.ParametersXmlLoad(oDoc,oXMLFile)
	
	'Update the file.
	iLogicVb.UpdateWhenDone = True
	
	MessageBox.Show("Parameter Import Complete", "iLogic")
End Sub


Function clean_filename(StyleName As String, replace_with As String)
    Dim inv_chars As String
    Dim cpos As Long
	
	'Origional List adjust function contents in invchars as needed.
	'"'!""#¤%&/()=?`^*>;:@£${[]}|~\,.'¨´+-"
	
    invchars = "'!""#¤%&/=?`^*>;:@£${[]}|~\,.'¨´+"
    For cpos = 1 To Len(invchars)
        StyleName = Replace(StyleName, Mid(invchars, cpos, 1), replace_with)
    Next
    clean_filename = StyleName
End Function

 

 

 

 

 

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

Aidarhanovn
Contributor
Contributor

Hi all
how to change pipe style using Inventor API?

0 Likes
Message 6 of 9

A.Acheson
Mentor
Mentor

There is unfortuantely no direct way to do this. I have manged to change styles using VBA mouse movements.

  1. Steps are extract styles as a list
  2. Input a style name and find its index.
  3. Use ControlDefinition to launch fly out styles list.
  4. Launch mouse moment macro to scroll the list to index. 

This is far from perfect and relies heavily on having a consistent styles list, with no on the fly changing of the style list otherwise the index is off. For this reason I never did use it in practice as style list were too inconstent. I can dig out the code if you want to attempt to use it. 

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

Tom_Fuller
Contributor
Contributor
Accepted solution

I Haven't tried this myself yet, but have you tried this:

  • Get the ControlDefiniton of the pipe style control
  • Cast it to a ComboBoxDefinition object
  • Set the ListIndex property of the ComboBoxDefinition to the index of the pipe style you want?

 

Dim oDoc As Document = ThisDoc.Document

Dim oCDs As ControlDefinitions = ThisApplication.CommandManager.ControlDefinitions 
	
'Active Style (Combo Box Tube and Pipe Styles)
Dim oCD As ControlDefinition = oCDs("HSL:Piping:StylesComboBox")

'Cast to ComboBoxDefinition.
Dim oComboBoxDef As ComboBoxDefinition = DirectCast(oCD, ComboBoxDefinition)

'Demo index that combobox will switch to, indices start at 1.
'Index should not be > oComboBoxDef.ListCount
Dim oDemoIndex As Integer = 3

'Set the index
oComboBoxDef.ListIndex = oDemoIndex

 

 

P.S. VB is not my language of choice, but I thought I would keep it consistent 😅 

Message 8 of 9

A.Acheson
Mentor
Mentor
Accepted solution

Hi @Tom_Fuller 

Thanks for supplying those lines they work great!! The breakthrough a lot of people were searching for.

 

The below code will retrieve the whole list of styles. 

 

    Dim oDoc As Document = ThisDoc.Document

	Dim oCDs As ControlDefinitions = ThisApplication.CommandManager.ControlDefinitions 
		
	'Active Style (Combo Box Tube and Pipe Styles).
	Dim oCD As ControlDefinition = oCDs("HSL:Piping:StylesComboBox")

	'Cast to ComboBoxDefinition.
	Dim oComboBoxDef As ComboBoxDefinition = DirectCast(oCD, ComboBoxDefinition)
	
	'Create a New List.
	Dim StyleList As New List(Of String) 
	
	For i = 1 To oComboBoxDef.ListCount
		StyleList.Add(oComboBoxDef.ListItem(i))
	Next
	
	Dim StyleName As String  = InputListBox("Prompt", StyleList, d0, Title := "Title", ListName := "List")

 

 

And here is a method to change all route styles from a list of styles

 

Sub Main()
	
	Dim StyleList As New List(Of String) 
	
	'Get Style List.
	StyleList = GetStyleList(StyleList)
	
	Dim StyleName As String  = InputListBox("Prompt", StyleList, d0, Title := "Title", ListName := "List")
 
	Dim oAssyDoc As AssemblyDocument = ThisApplication.ActiveDocument
	
	Dim oAsmCompDef As AssemblyComponentDefinition = oAssyDoc.ComponentDefinition 

	'Iterate through all of the occurrences at Level 1
	For Each oOcc As ComponentOccurrence In oAsmCompDef.Occurrences 
		
		Dim oOccDoc As Document = oOcc.Definition.Document
		
		' Get Tube and Pipe Top Level(TPAssy).
		Dim bIsTPAssyDoc As Boolean = oOccDoc.DocumentInterests.HasInterest("Piping Runs Environment")
		
		If bIsTPAssyDoc = True Then
			For Each oSubocc As ComponentOccurrence In oOcc.SubOccurrences
				If oSubocc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then 
					For Each oSubSubocc As ComponentOccurrence In oSubocc.SubOccurrences
						If oSubSubocc.Name.Contains("Route") = True Then
							oSubSubocc.Edit
							ChangeStyle(StyleName)
							oSubSubocc.Definition.Document.Update
						End If
					Next
				End If	
			Next
		End If
	Next

	oAsmCompDef.ActiveOccurrence.ExitEdit(ExitTypeEnum.kExitToTop)

End Sub

Function GetStyleList(StyleList As List(Of String))

	Dim oDoc As Document = ThisDoc.Document

	Dim oCDs As ControlDefinitions = ThisApplication.CommandManager.ControlDefinitions 
		
	'Active Style (Combo Box Tube and Pipe Styles).
	Dim oCD As ControlDefinition = oCDs("HSL:Piping:StylesComboBox")

	'Cast to ComboBoxDefinition.
	Dim oComboBoxDef As ComboBoxDefinition = DirectCast(oCD, ComboBoxDefinition)

	For i = 1 To oComboBoxDef.ListCount
		StyleList.Add(oComboBoxDef.ListItem(i))
	Next
	
	Return StyleList
	
End Function

Sub ChangeStyle(StyleName As String)

	Dim oDoc As Document = ThisDoc.Document

	Dim oCDs As ControlDefinitions = ThisApplication.CommandManager.ControlDefinitions 
		
	'Active Style (Combo Box Tube and Pipe Styles)
	Dim oCD As ControlDefinition = oCDs("HSL:Piping:StylesComboBox")

	'Cast to ComboBoxDefinition.
	Dim oComboBoxDef As ComboBoxDefinition = DirectCast(oCD, ComboBoxDefinition)

	For i = 1 To oComboBoxDef.ListCount
		 If oComboBoxDef.ListItem(i) = StyleName
			'Set the index
			oComboBoxDef.ListIndex = i
		 Exit For
		End If
	Next
End Sub

 

 

 

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

Tom_Fuller
Contributor
Contributor

Hi @A.Acheson,

I'm glad I can help! Spent a lot of time working the Tube & Pipe, figuring out how it interacts with the API, so if you need any other help feel free to give me a shout 😀