Wanting to add an image to an existing title block in a drawing set & defined the following:
Dim oTitleBlockDef As Inventor.TitleBlockDefinition oTitleBlockDef = oDrawDoc.TitleBlockDefinition
Ref: (oDrawDoc = ThisApplication.ActiveDocument)
Get the following run-time error when trying to run the iLogic routine:
Error on line 7 in rule: Insert Image 2, in document: xxxx.idw
Public member 'TitleBlockDefinition' on type '_DocumentClass' not found.
What is proper way to define the name of the existing Title Block to the code?
Thanks in advance...
Solved! Go to Solution.
Solved by WCrihfield. Go to Solution.
Hi @mwilsonUG99E. Here is just a partial example iLogic code that points out where all the title block definition related stuff is.
Sub Main
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub
Dim oDDoc As DrawingDocument = ThisDoc.Document
Dim oTBDefs As TitleBlockDefinitions = oDDoc.TitleBlockDefinitions
If oTBDefs.Count = 0 Then Exit Sub
Dim oFirstTBDef As TitleBlockDefinition = oTBDefs.Item(1)
Dim oSheets As Inventor.Sheets = oDDoc.Sheets
For Each oSheet As Inventor.Sheet In oSheets
If oSheet.TitleBlock IsNot Nothing Then
Dim oSheetTB As TitleBlock = oSheet.TitleBlock
Dim oTBDef As TitleBlockDefinition = oSheetTB.Definition
'and so on
End If
Next 'oSheet
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield
(Not an Autodesk Employee)
Thank you @WCrihfield ! One further question - I need to access the sketch that defines the title block to insert a company logo (person doing the drawing will select logo image from an on-disk library) into that sketch & save back. Do you happen to have code that will accomplish that?
Thank You again!
Hi @mwilsonUG99E. Yes, I have a simple iLogic rule for adding a SketchImage into the DrawingSketch of a TitleBlockDefinition. It can be tricky, because its sketch is normally ReadOnly, but you can use the Edit method to get a copy of the original sketch that is Read/Write, then when done making changes, you can use the ExitEdit method to save that edited copy of the sketch back over the original. Since I do not know what your title block definition is called, and don't know which one you want to edit, I left two lines of code in there (one simply getting the first one by its Index, and one that gets one by its name). If needed, you could change it to show you a list of available ones to choose from. I also included a fairly simple custom Function, which will allow you to use the Windows file browser to browse for the image file you want to add. Right now, it will just insert it at the bottom left corner of the sheet though, so you may want to either change that, or manually edit the TitleBlockDefinition's sketch afterwards to manually scale and position it where you want it. Also, it is currently set to 'Link' the image file, so if you do not want the image file to be linked to the drawing document, you can change the value of the bLingImage variable to False.
Sub Main
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub
Dim oDDoc As DrawingDocument = ThisDoc.Document
Dim oTBDefs As TitleBlockDefinitions = oDDoc.TitleBlockDefinitions
If oTBDefs.Count = 0 Then Exit Sub
Dim oTBDef As TitleBlockDefinition = oTBDefs.Item(1)
'Dim oTBDef As TitleBlockDefinition = oTBDefs.Item("NameOfTitleBlockDefinition")
Dim oDSketch As DrawingSketch = Nothing
'this next line sets that variable's value with a copy of the original sketch, for editing
oTBDef.Edit(oDSketch)
Dim oSImages As SketchImages = oDSketch.SketchImages
'Dim sImageFullFileName As String = "C:\Temp\MyImage.jpg"
Dim sImageFullFileName As String = BrowseForFile
If sImageFullFileName = "" Then Exit Sub 'empty String returned from function, so exit rule
'<<< CHANGE LOCATION AS NEEDED >>>
Dim oPosition As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)
Dim bLinkImage As Boolean = True '<<< IF YOU DON'T WANT LINKED, SET TO FALSE >>>
Dim oSImage As SketchImage = Nothing
Try
oSImage = oSImages.Add(sImageFullFileName, oPosition, bLinkImage)
Catch oEx As Exception
MsgBox("Error adding SketchImage to TextBoxDefinition's Sketch" & vbCrLf & _
oEx.Message & vbCrLf & oEx.StackTrace, vbCritical, "Error Adding Image")
End Try
oTBDef.ExitEdit(True) 'True = save this edited copy over original sketch
oDDoc.ActiveSheet.Update
oDDoc.Update2(True)
End Sub
Function BrowseForFile() As String
Dim oOpenDlg As New System.Windows.Forms.OpenFileDialog
oOpenDlg.Title = "Select A File."
oOpenDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
oOpenDlg.Filter = "All files (*.*)|*.*"
oOpenDlg.Multiselect = False
oOpenDlg.RestoreDirectory = False
Dim oResult As System.Windows.Forms.DialogResult = oOpenDlg.ShowDialog
If oResult = System.Windows.Forms.DialogResult.OK Then Return oOpenDlg.FileName
Return Nothing 'only reaches here if canceled or nothing selected
End Function
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield
(Not an Autodesk Employee)
Can't find what you're looking for? Ask the community or share your knowledge.