ilogic to load a style in templates automatically

ilogic to load a style in templates automatically

Anonymous
Not applicable
967 Views
5 Replies
Message 1 of 6

ilogic to load a style in templates automatically

Anonymous
Not applicable

Hi,

 

I work with different templates at a time. I want to know if there are any ilogic so that I can automatically load a style onto all the templates I have so that I don't have to each time go to style editor to change the style.

Thanks.

 

Anas.

0 Likes
968 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Can you be a little more specific?  Are you talking about multiple drawing templates only, multiple part templates only, multiple assembly templates only, or a mix of drawing, part, & assembly templates?  What all specific kinds of styles are you talking about?  How would you want to specify which Styles you want to add into the Template files (manually enter them into the code somehow, or use an InputBox to allow you to enter the styles names and types, etc.)?  The code wold need to know what type of style it is, before it can add it into the template file.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

Anonymous
Not applicable

I am talking about drawing templates only. I want to enter the style name directly in the ilogic , the input box also will work just fine. 

0 Likes
Message 4 of 6

WCrihfield
Mentor
Mentor

Are you wanting to copy styled from the currently active drawing document back to the template drawing documents?

If so, do you want these styles to be added into the global repository (style library), so that all older drawings can have access to it, or do you want the new styles to just be local within the template files afterwords?  Do you want these new styles to be set as the default ones to be used for when you create new items of that type from those template files?

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 6

JhoelForshav
Mentor
Mentor

Hi @Anonymous 

Are you looking for something like this?

Dim oDoc As DrawingDocument = ThisDoc.Document
Dim oList As New List(Of String)
For Each oStyle As DrawingStandardStyle In oDoc.StylesManager.StandardStyles
oList.Add(oStyle.Name)
Next
Dim oStyleName As String = InputListBox("Select Style", oList, oList(0))
oDoc.StylesManager.ActiveStandardStyle = oDoc.StylesManager.StandardStyles.Item(oStyleName)

You can have the rule trigger on new document in your drawing template 🙂

Message 6 of 6

WCrihfield
Mentor
Mentor

Here's some code to:

  • allow you to enter a name into an InputBox
  • searches all styles within the active document for a style by that name to get the Style object
  • Saves that style to the global repository (style library)
  • Finds all drawing template files within the current projects templates location
  • Opens each one, then searches for a style by that name within it, then checks to see if it is located globally, locally, or both.
    • If it is located globally only, it creates a local copy of it within the template file
    • If it is located either locally or both, it lets you know and asks you if you want to replace the existing local one within the template.
      • If yes, it replaces it with the one from your current document.
  • Saves, then closes the template document

With several checks in there to help eliminate possible error messages.

See if this will work for you.

Dim oDDoc As DrawingDocument = ThisDrawing.Document

'Get Style name from user
Enter :
Dim oStyleName As String = InputBox("Enter the Name of the Style you want to copy to the Templates.", "STYLE NAME")
If oStyleName = "" Then
	oAns = MsgBox("Nothing was entered. Try again?", vbYesNo + vbQuestion, "NO ENTRY")
	If oAns = vbYes Then GoTo Enter Else Return
End If

'Search for this style in the local drawing document, to get the actual Style object (early check)
Dim oFound As Boolean = False
Dim oStyle As Style
For Each oDStyle As Style In oDDoc.StylesManager.Styles
	If oDStyle.Name = oStyleName Or oDStyle.InternalName = oStyleName Then
		oFound = True
		oStyle = oDStyle
		oStyle.SaveToGlobal
	End If
Next
If oFound = False Then
	oTryAgain = MsgBox("No Style was found with that name. Try again?", vbYesNo + vbQuestion, "STYLE NOT FOUND")
	If oTryAgain = vbYes Then GoTo Enter Else Return
End If

'Get all the Drawing Template files
Dim oTPath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.TemplatesPath
Dim oFileNames() As String = System.IO.Directory.GetFiles(oTPath, "*.idw", IO.SearchOption.TopDirectoryOnly)
'Check if multiple template files were found.
If oFileNames.Length = 0 Then  'it did not find any
	MsgBox("No template files were found matching your search criteria. Exiting.", vbOKOnly, "NOTHING FOUND")
	Return
ElseIf oFileNames.Length > 0 Then  'it found some
	a = InputListBox("There were " & oFileNames.Length & " files found matching your search criteria." & vbCrLf & "Here is the list:", oFileNames)
End If

'Open each drawing template file as a drawing document, then copy specified style library styles to local.
Dim oFName As String
Dim oTDDoc As DrawingDocument
Dim oTStyle As Style
For Each oFName In oFileNames
	oTDDoc = ThisApplication.Documents.Open(oFName, False)
	For Each oTStyle In oTDDoc.StylesManager.Styles
		If oTStyle.Name = oStyleName Or oTStyle.InternalName = oStyleName Then
			If oTStyle.StyleLocation = StyleLocationEnum.kLibraryStyleLocation Then
				oTStyle.ConvertToLocal
			ElseIf oTStyle.StyleLocation = StyleLocationEnum.kBothStyleLocation Or _
				oTStyle.StyleLocation = StyleLocationEnum.kLocalStyleLocation Then
				oReplace = MsgBox("That style already exists locally within this template file." & vbCrLf & _
				"Do you want to replace the existing one?", vbYesNo + vbQuestion, "STYLE EXISTS")
				If oReplace = vbYes Then
					Dim oTSCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
					oTSCol.Add(oTStyle)
					oTDDoc.StylesManager.ReplaceStyles(oTSCol, oStyle, True) 'True purges old version
				End If
			End If
		End If
	Next
	oTDDoc.Save
	oTDDoc.Close
Next

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes