Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Add Stamp or Change drawing border with ilogic???

6 REPLIES 6
Reply
Message 1 of 7
Jason.Rugg
515 Views, 6 Replies

Add Stamp or Change drawing border with ilogic???

Below I have combined code for creating a pdf and printing and drawing. I need to add code to do a border swap with prompted entry text and then after creating the pdf and printing revert the border back to the original. So ideally I would run the rule and it would switch to a border with prompted entry text for Signature, Date, Job No, and Rev. The user would fill out that info and then the rule would continue and create the pdf and then print, after that the border would revert back to the original. Also it would be nice if during the prompted text entry, if a rev is entered that is anything other than a "blank, zero or an A" the code would add an additional static stamp onto the drawing as another visual indicator that the drawing has been revised.

 

If a border swap isn't doable just adding a stamp with the prompted entry text before pdf and printing would work as well but would still need the additional static stamp if an additional rev is entered. Then after pdf and printing the code would remove the stamps.

 

Edit: The more I think about it, it would probably make more sense to use a stamp instead of swapping out drawing borders because the drawing templates we use vary.

Option Explicit On
Imports System.IO


Sub Main	
	CreatePDF()
	Printing()
End Sub

Sub CreatePDF()
	
	Dim oDrgDoc As DrawingDocument = ThisDoc.Document
	
	iProperties.Value("Custom", "PlotDate&Time") = Now.ToString()	

	' Set reference to drawing print manager' DrawingPrintManager has more options than PrintManager' as it's specific to drawing document
	Dim oDrgPrintMgr As DrawingPrintManager = oDrgDoc.PrintManager
	Dim pdfname = ThisDoc.FileName(False)
	Dim filePath = ThisDoc.Path

	' Set the printer name' comment this line to use default printer or assign another one
	oDrgPrintMgr.Printer = "Microsoft Print To PDF"

	'Set the paper size , scale and orientation
	oDrgPrintMgr.ScaleMode = PrintScaleModeEnum.kPrintBestFitScale
	oDrgPrintMgr.PaperSize = PaperSizeEnum.kPaperSize11x17
	oDrgPrintMgr.PrintRange = PrintRangeEnum.kPrintAllSheets
	oDrgPrintMgr.Orientation = kLandscapeOrientation
	'oDrgPrintMgr.PaperSource = 2
	oDrgPrintMgr.AllColorsAsBlack = False

	Dim pdfFileName = System.IO.Path.Combine(filePath, pdfname + ".pdf")
	
	If Not CanSaveToFile(pdfFileName) Then
		MessageBox.Show("Folder or file is read only", "Save PDF")

		Dim oFileDlg As Inventor.FileDialog 
		Call ThisApplication.CreateFileDialog(oFileDlg)
		
		oFileDlg.FilterIndex = 1 

	  	oFileDlg.DialogTitle = "Save file" 

	  	'oFileDlg.InitialDirectory = 
	  	oFileDlg.CancelError = False  
		
		oFileDlg.FileName = pdfname + ".pdf"
	 	Call oFileDlg.ShowSave() 
		
		pdfFileName = oFileDlg.FileName
		If String.IsNullOrEmpty(pdfFileName) Then 
			Logger.Info("No file specified")
			Return
		End If
	End If

	oDrgDoc.SaveAs(pdfFileName, True) 	

End Sub

Sub Printing()
	
	Dim oDrgDoc As DrawingDocument = ThisDoc.Document
	
	'Plot Stamp
	'Applies current filename & path, Date & time, and current user stamp to edge of border to be printed with the drawing
	'[
	iProperties.Value("Custom", "PlotDate&Time") = Now
	iProperties.Value("Custom", "CurrentUser") = System.Security.Principal.WindowsIdentity.GetCurrent.Name

	InventorVb.DocumentUpdate()

	'Dim oCtrlDef As ControlDefinition
	'oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("AppFilePrintCmd")
	'oCtrlDef.Execute
	']

	'Print Setup
	'[

	' Set reference to drawing print manager' DrawingPrintManager has more options than PrintManager' as it's specific to drawing document
	Dim oDrgPrintMgr As DrawingPrintManager = oDrgDoc.PrintManager
	' Set the printer name' comment this line to use default printer or assign another one
	'oDrgPrintMgr.Printer = "\\kcidc2012\EngineeringSharp_Ledger"
	'oDrgPrintMgr.Printer = "EngineeringSharp_Ledger"
	'oDrgPrintMgr.Printer = "\\kcidc2012\EngineeringSharp_Ledger_Color"
	oDrgPrintMgr.Printer = "\\kcipdc2019\Engineering_Ledger_Color"

	'Set the paper size , scale and orientation
	'oDrgPrintMgr.ScaleMode = PrintScaleModeEnum.kPrintCustomScale
	oDrgPrintMgr.ScaleMode = PrintScaleModeEnum.kPrintBestFitScale
	'oDrgPrintMgr.PaperSize = PaperSizeEnum.kPaperSizeLedger
	'oDrgPrintMgr.PaperSize = PaperSizeEnum.14338
	'oDrgPrintMgr.PaperSize = PaperSizeLedger
	oDrgPrintMgr.PrintRange = kPrintCurrentSheet
	oDrgPrintMgr.Orientation = kLandscapeOrientation
	oDrgPrintMgr.PaperSource = 2
	oDrgPrintMgr.AllColorsAsBlack = False
	oDrgPrintMgr.SubmitPrint
	']
End Sub


Function CanSaveToFile(fileName As String)
	Dim FileInfo As New FileInfo(fileName)
	If FileInfo.Exists Then
		If FileInfo.IsReadOnly Then
			Return False
		ElseIf FileIsLocked(FileInfo) Then
			' We can't write to it. It might be open in Adobe Acrobat Reader
			Return False
		End If
	End If
	
	' make sure we can write to the directory
	Dim folderName = System.IO.Path.GetDirectoryName(fileName)
	Dim tempName = System.IO.Path.Combine(folderName, System.Guid.NewGuid().ToString() + ".txt")
	Try
		System.IO.File.WriteAllText(tempName, "test")
		System.IO.File.Delete(tempName)
	Catch
		Return False
	End Try
		
	Return True		
End Function

' from https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use
Function FileIsLocked(FileInfo As FileInfo) As Boolean

    Dim Stream As FileStream = Nothing

    Try
        Stream = FileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.None)
    Catch ex As IOException
        'the file is unavailable because it is:
        'still being written To
        'or being processed By another thread
        'or does Not exist (has already been processed)
        Return True
    Finally
        If (Stream IsNot Nothing) Then
            Stream.Close()
		End If
	End Try
		
    Return False
End Function
6 REPLIES 6
Message 2 of 7
WCrihfield
in reply to: Jason.Rugg

Hi @Jason.Rugg.  One way to tackle this kind of situation might be to simply create a copy of the drawing, then make all the needed changes to that copy, export & print it, then delete that copy, leaving your original unchanged.  This would likely be much less challenging to set-up than trying to retain lots of original data, changing borders/stamps twice, and restoring original data back in place later.  Just a thought.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7
Jason.Rugg
in reply to: WCrihfield

@WCrihfield I don't disagree with you, we essentially just need a one button click with minimal user interface that's simple. Currently it is making the pdf and printing, just need to get it to add the stamp(s) beforehand and then remove them after. It can even be simplified more I think by instead of it trying to detect a rev, have it pop up a question and ask if it is a revised drawing and if "yes" it adds the additional stamp.

 

If I can get this to work, the user clicks the button, fills in the text, it pdf's and prints and then maybe at the very end when its done it displays a completed message and reminds the user to place a copy of the pdf in vault.

Message 4 of 7

Hi @Jason.Rugg 

 

See these examples.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

 

Sub main
	Call PlaceStamp
End Sub

Sub PlaceStamp
	' reference to the sketched symbol definition.
	Dim oSymbolDef As SketchedSymbolDefinition
	oSymbolDef = ThisDrawing.Document.SketchedSymbolDefinitions.Item("Release Stamp")

	Dim oSheet As Sheet = ThisDrawing.Document.ActiveSheet

	'create insertion point, coordinates - in cm !
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	Dim oPoint As Point2d = oTG.CreatePoint2d(10, 10)

	Dim sPromptStrings(3) As String
	'get prompt values, this example just uses hardcoded values
	sPromptStrings(0) = "CDW" 'Signature
	sPromptStrings(1) = DateString 'Date
	sPromptStrings(2) = "12345" 'Job
	sPromptStrings(3) = "1.1" 'Rev

	' Add an instance of the sketched symbol to the sheet.
	' Rotate angle = 0 radians,  scale = 1 
	Dim oSymbol As SketchedSymbol 
	oSymbol = oSheet.SketchedSymbols.Add(oSymbolDef, oPoint, 0, 1, sPromptStrings)
	
	Call PrintPDF	
	oSymbol.Delete
	
End Sub

Sub PrintPDF
	'your PDF code here
	MsgBox("PDF")
End Sub

 

 

 

 

 

 

Sub main
	Call SwapBorder
End Sub

Sub SwapBorder
	'get current border
	oCurrentBorderName = ThisDrawing.ActiveSheet.Border.ToString

	oNewBorderName = "Release Border"

	'get prompt values, this example just uses hardcoded values
	oPromptedEntry1 = "CDW" 'Signature
	oPromptedEntry2 = DateString 'Date
	oPromptedEntry3 = "12345" 'Job
	oPromptedEntry4 = "1.1" 'Rev

	'place new border ( with prompted entry
	ActiveSheet.SetBorder(oNewBorderName, oPromptedEntry1, oPromptedEntry2, oPromptedEntry3, oPromptedEntry4)

	Call PrintPDF

	'place original border (no prompted entry)
	ActiveSheet.SetBorder(oCurrentBorderName)

End Sub

Sub PrintPDF
	'your PDF code here
	MsgBox("PDF")
End Sub

 

 

Message 5 of 7

Hey @Curtis_Waguespack I have been working with the PlaceStamp example you showed. I have it incorporated into my code and is working with hardcoded entry. How do I make them prompted entry? Also when I print it makes a print with the stamp and a second without the stamp, I don't need the second print, how do I adjust the code to fix that? 

 

Option Explicit On
Imports System.IO

 

Sub Main
	PlaceStamp()
	CreatePDF()
	Printing()
End Sub

Sub PlaceStamp
	' reference to the sketched symbol definition.
	Dim oSymbolDef As SketchedSymbolDefinition
	oSymbolDef = ThisDrawing.Document.SketchedSymbolDefinitions.Item("KCI APPROVED FOR FABRICATION STAMP")

	Dim oSheet As Sheet = ThisDrawing.Document.ActiveSheet

	'create insertion point, coordinates - in cm !
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	Dim oPoint As Point2d = oTG.CreatePoint2d(10, 10)

	' Add an instance of the sketched symbol definition to the sheet.
	' Rotate angle = 0 radians,  scale = 1 when adding

	Dim sPromptStrings(3) As String
	'get prompt values, this example just uses hardcoded values
	sPromptEntry1(0) = "CDW" 'Signature
	sPromptStrings(1) = DateString 'Date
	sPromptStrings(2) = "12345" 'Job
	sPromptStrings(3) = "1.1" 'Rev

	Dim oSymbol As SketchedSymbol 
	oSymbol = oSheet.SketchedSymbols.Add(oSymbolDef, oPoint, 0, 1, sPromptStrings)
	
	Call CreatePDF	
	Call Printing
	oSymbol.Delete
	
End Sub

Sub CreatePDF()
	
	Dim oDrgDoc As DrawingDocument = ThisDoc.Document
	
	iProperties.Value("Custom", "PlotDate&Time") = Now.ToString()	

	' Set reference to drawing print manager' DrawingPrintManager has more options than PrintManager' as it's specific to drawing document
	Dim oDrgPrintMgr As DrawingPrintManager = oDrgDoc.PrintManager
	Dim pdfname = ThisDoc.FileName(False)
	Dim filePath = ThisDoc.Path

	' Set the printer name' comment this line to use default printer or assign another one
	oDrgPrintMgr.Printer = "Microsoft Print To PDF"

	'Set the paper size , scale and orientation
	oDrgPrintMgr.ScaleMode = PrintScaleModeEnum.kPrintBestFitScale
	oDrgPrintMgr.PaperSize = PaperSizeEnum.kPaperSize11x17
	oDrgPrintMgr.PrintRange = PrintRangeEnum.kPrintAllSheets
	oDrgPrintMgr.Orientation = kLandscapeOrientation
	'oDrgPrintMgr.PaperSource = 2
	oDrgPrintMgr.AllColorsAsBlack = False

	Dim pdfFileName = System.IO.Path.Combine(filePath, pdfname + ".pdf")
	
	If Not CanSaveToFile(pdfFileName) Then
		MessageBox.Show("Folder or file is read only", "Save PDF")

		Dim oFileDlg As Inventor.FileDialog 
		Call ThisApplication.CreateFileDialog(oFileDlg)
		
		oFileDlg.FilterIndex = 1 

	  	oFileDlg.DialogTitle = "Save file" 

	  	'oFileDlg.InitialDirectory = 
	  	oFileDlg.CancelError = False  
		
		oFileDlg.FileName = pdfname + ".pdf"
	 	Call oFileDlg.ShowSave() 
		
		pdfFileName = oFileDlg.FileName
		If String.IsNullOrEmpty(pdfFileName) Then 
			Logger.Info("No file specified")
			Return
		End If
	End If

	oDrgDoc.SaveAs(pdfFileName, True) 	

End Sub

Sub Printing()
	
	Dim oDrgDoc As DrawingDocument = ThisDoc.Document
	
	'Plot Stamp
	'Applies current filename & path, Date & time, and current user stamp to edge of border to be printed with the drawing
	'[
	iProperties.Value("Custom", "PlotDate&Time") = Now
	iProperties.Value("Custom", "CurrentUser") = System.Security.Principal.WindowsIdentity.GetCurrent.Name

	InventorVb.DocumentUpdate()

	'Dim oCtrlDef As ControlDefinition
	'oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("AppFilePrintCmd")
	'oCtrlDef.Execute
	']

	'Print Setup
	'[

	' Set reference to drawing print manager' DrawingPrintManager has more options than PrintManager' as it's specific to drawing document
	Dim oDrgPrintMgr As DrawingPrintManager = oDrgDoc.PrintManager
	' Set the printer name' comment this line to use default printer or assign another one
	'oDrgPrintMgr.Printer = "\\kcidc2012\EngineeringSharp_Ledger"
	'oDrgPrintMgr.Printer = "EngineeringSharp_Ledger"
	'oDrgPrintMgr.Printer = "\\kcidc2012\EngineeringSharp_Ledger_Color"
	oDrgPrintMgr.Printer = "\\kcipdc2019\Engineering_Ledger_Color"

	'Set the paper size , scale and orientation
	'oDrgPrintMgr.ScaleMode = PrintScaleModeEnum.kPrintCustomScale
	oDrgPrintMgr.ScaleMode = PrintScaleModeEnum.kPrintBestFitScale
	'oDrgPrintMgr.PaperSize = PaperSizeEnum.kPaperSizeLedger
	'oDrgPrintMgr.PaperSize = PaperSizeEnum.14338
	'oDrgPrintMgr.PaperSize = PaperSizeLedger
	oDrgPrintMgr.PrintRange = kPrintCurrentSheet
	oDrgPrintMgr.Orientation = kLandscapeOrientation
	oDrgPrintMgr.PaperSource = 2
	oDrgPrintMgr.AllColorsAsBlack = False
	oDrgPrintMgr.SubmitPrint
	']
End Sub


Function CanSaveToFile(fileName As String)
	Dim FileInfo As New FileInfo(fileName)
	If FileInfo.Exists Then
		If FileInfo.IsReadOnly Then
			Return False
		ElseIf FileIsLocked(FileInfo) Then
			' We can't write to it. It might be open in Adobe Acrobat Reader
			Return False
		End If
	End If
	
	' make sure we can write to the directory
	Dim folderName = System.IO.Path.GetDirectoryName(fileName)
	Dim tempName = System.IO.Path.Combine(folderName, System.Guid.NewGuid().ToString() + ".txt")
	Try
		System.IO.File.WriteAllText(tempName, "test")
		System.IO.File.Delete(tempName)
	Catch
		Return False
	End Try
		
	Return True		
End Function

' from https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use
Function FileIsLocked(FileInfo As FileInfo) As Boolean

    Dim Stream As FileStream = Nothing

    Try
        Stream = FileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.None)
    Catch ex As IOException
        'the file is unavailable because it is:
        'still being written To
        'or being processed By another thread
        'or does Not exist (has already been processed)
        Return True
    Finally
        If (Stream IsNot Nothing) Then
            Stream.Close()
		End If
	End Try
		
    Return False
End Function

 

 

Message 6 of 7

Hi @Jason.Rugg 

 

See this updated example.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Sub main
	Call PlaceStamp
	Call CreatePDF
	Call Printing
	Call DeleteStamp
End Sub

Sub PlaceStamp
	' reference to the sketched symbol definition.
	Dim oSymbolDef As SketchedSymbolDefinition
	oSymbolDef = ThisDrawing.Document.SketchedSymbolDefinitions.Item("Release Stamp")

	Dim oSheet As Sheet = ThisDrawing.Document.ActiveSheet

	'create insertion point, coordinates - in cm !
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	Dim oPoint As Point2d = oTG.CreatePoint2d(10, 10)

	Dim sPromptStrings(3) As String

	sPromptStrings(0) = InputBox("Enter Signature", "iLogic", "")
	sPromptStrings(1) = InputBox("Enter Date", "iLogic", "")
	sPromptStrings(2) = InputBox("Enter Job #", "iLogic", "")
	sPromptStrings(3) = InputBox("Enter Rev", "iLogic", "")

	' Add an instance of the sketched symbol to the sheet.
	' Rotate angle = 0 radians,  scale = 1 
	Dim oSymbol As SketchedSymbol
	oSymbol = oSheet.SketchedSymbols.Add(oSymbolDef, oPoint, 0, 1, sPromptStrings)
End Sub

Sub DeleteStamp

	Dim oSymbol As SketchedSymbol
	For Each oSymbol In ThisDrawing.Document.ActiveSheet.SketchedSymbols
		If oSymbol.Name = "Release Stamp" Then oSymbol.Delete
	Next
End Sub

Sub CreatePDF
	'your PDF code here
	MsgBox("PDF")
End Sub

Sub Printing
	'your printng code here
	MsgBox("Printing")
End Sub
Message 7 of 7
J-Camper
in reply to: Jason.Rugg

@Jason.Rugg, I'm not sure if you have had success with @Curtis_Waguespack 's latest update, but I have a workflow suggestion which might require less iLogic.

 

We had a RFP "Stamp" that was requested for our drawings to clearly identify if a drawing on the shop floor is true RFP status.  Instead of creating a sketch symbol with prompted entries, I created a custom iProperty and set it up in my Border sketch where I wanted it to show up.  When the iProperty is left blank, it appears as if it don't exist.  Then when you add text to the iProperty, it populates to every sheet because they all use the same border.  I do this manually because I just have 1 entry and don't want to revert my drawing, but you could set up a form linked to the iProperty values for the user to fill out then press a "Process" button which calls an iLogic rule to print and then clear the iProperty values to return it to original. 

 

I'm not sure if you work with multi-sheet drawings, but I found this approach to be very easy to manage for the multi-sheet drawings we produce. 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report