Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
WCrihfield
in reply to: mwilsonUG99E

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) :thumbs_up:.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)