Use iLogic to set layer color and line style when exporting image

Use iLogic to set layer color and line style when exporting image

blmartin
Participant Participant
364 Views
6 Replies
Message 1 of 7

Use iLogic to set layer color and line style when exporting image

blmartin
Participant
Participant

We started adding additional sheets to assembly prints to use as work instructions. We've made a code snippet (below) that will export an image directly from Inventor so we don't have to flatten and convert in an image editing app. The code will capture paper color, set it to white while it makes the image, and then sets paper color back to what it was.

 

I'd like to do the same thing with tweak trails; make the layer red, line style as double dash chain, and line weight of .039. Can I add that to the code I have?

 

Dim Doc As DrawingDocument = ThisApplication.ActiveDocument
Dim Path As String = ThisDoc.Path
Dim Name As String = ThisDoc.FileName(False)
Dim Sheet As String = ActiveSheet.Name
Sheet = Replace(Sheet, ":", "_")
Dim ImagePath As String = Path & "\" & Name & "_" & Sheet & ".png"
Dim OriginalColor As Color = Doc.SheetSettings.SheetColor
Dim White As Color = ThisApplication.TransientObjects.CreateColor(255,255,255)
Dim H1 = ThisDrawing.ActiveSheet.Height * 150
Dim W1 = ThisDrawing.ActiveSheet.Width * 150

' Message box used when error proofing
' MessageBox.Show(ImagePath, "This is where we are trying to save", MessageBoxButtons.OK)

ThisApplication.CommandManager.ControlDefinitions.Item("AppZoomAllCmd").Execute
Doc.SheetSettings.SheetColor = White
ThisApplication.ActiveView.SaveAsBitmap(ImagePath, W1, H1)
Doc.SheetSettings.SheetColor = OriginalColor

 

0 Likes
365 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @blmartin.  Here is a modified version of your code, with the added functionality added in.  I also changed a couple things to either make them more efficient, or to help avoid potential/foreseen problems.

Dim oInvApp As Inventor.Application = ThisApplication
Dim Doc As DrawingDocument = oInvApp.ActiveDocument
'get full path and file name, without file extension, from DrawingDocument object above
Dim sPathAndName As String = System.IO.Path.ChangeExtension(Doc.FullFileName, vbNullString)
Dim oASheet As Inventor.Sheet = Doc.ActiveSheet
Dim sSheetName As String = oASheet.Name
sSheetName = Replace(sSheetName, ":", "_")
Dim ImagePath As String = sPathAndName & "_" & sSheetName & ".png"
Dim oShtSettings As Inventor.SheetSettings = Doc.SheetSettings
Dim OriginalColor As Inventor.Color = oShtSettings.SheetColor
Dim oTO As TransientObjects = oInvApp.TransientObjects
Dim White As Inventor.Color = oTO.CreateColor(255, 255, 255)
Dim Red As Inventor.Color = oTO.CreateColor(255, 0, 0)
Dim H1 As Double = oASheet.Height * 150
Dim W1 As Double = oASheet.Width * 150
Dim oLayer As Inventor.Layer = Doc.StylesManager.Layers.Item("Tweak Trail (ANSI)")
oLayer.Color = Red
oLayer.LineType = LineTypeEnum.kDoubleDashedChainLineType
oLayer.LineWeight = (2.54/1000)*39 'wants centimeters, instead of inches
' Message box used when error proofing
' MessageBox.Show(ImagePath, "This is where we are trying to save", MessageBoxButtons.OK)
Dim oView As Inventor.View = oInvApp.ActiveView
oView.Fit
oShtSettings.SheetColor = White
oView.SaveAsBitmap(ImagePath, W1, H1)
oShtSettings.SheetColor = OriginalColor

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

EESignature

(Not an Autodesk Employee)

Message 3 of 7

blmartin
Participant
Participant

Thank you for the response. Your code does most of it. The exported image does have layer color as red and has the double dash chain line type. The exported image isn't zommed into extents, and the line weight stays .010.

 

After it makes the image, it zooms and sets the line weight. I've tried reordering things, but I don't know how to tell Inventor to export the image at a different time.

 

I added a line to define black, as at the end I want to turn the layer to black again, and added a line to change the line type back, since we won't want these settings on normal factory drawings.

 

Dim oInvApp As Inventor.Application = ThisApplication
Dim Doc As DrawingDocument = oInvApp.ActiveDocument
'get full path and file name, without file extension, from DrawingDocument object above
Dim sPathAndName As String = System.IO.Path.ChangeExtension(Doc.FullFileName, vbNullString)
Dim oASheet As Inventor.Sheet = Doc.ActiveSheet
Dim sSheetName As String = oASheet.Name
sSheetName = Replace(sSheetName, ":", "_")
Dim ImagePath As String = sPathAndName & "_" & sSheetName & ".png"
Dim oShtSettings As Inventor.SheetSettings = Doc.SheetSettings
Dim OriginalColor As Inventor.Color = oShtSettings.SheetColor
Dim oTO As TransientObjects = oInvApp.TransientObjects
Dim White As Inventor.Color = oTO.CreateColor(255, 255, 255)
Dim Red As Inventor.Color = oTO.CreateColor(255, 0, 0)
Dim Black As Inventor.Color = oTO.CreateColor(0, 0, 0)
Dim H1 As Double = oASheet.Height * 150
Dim W1 As Double = oASheet.Width * 150
Dim oLayer As Inventor.Layer = Doc.StylesManager.Layers.Item("Tweak Trail (ANSI)")
oLayer.Color = Red
oLayer.LineType = LineTypeEnum.kDoubleDashedChainLineType
oLayer.LineWeight = (2.54/1000)*39 'wants centimeters, instead of inches
' Message box used when error proofing
' MessageBox.Show(ImagePath, "This is where we are trying to save", MessageBoxButtons.OK)
Dim oView As Inventor.View = oInvApp.ActiveView
oView.Fit
oShtSettings.SheetColor = White
oView.SaveAsBitmap(ImagePath, W1, H1)
oShtSettings.SheetColor = OriginalColor
oLayer.Color = Black
oLayer.LineType = LineTypeEnum.kContinuousLineType
oLayer.LineWeight = (2.54/1000)*10

 

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor

Interesting timing results & behavior.

One of the changes I made was switching out the "AppZoomAllCmd" command execution for a the View.Fit method.  I assumed it would work just the same, but apparently not.  You could also try View.Camera.SetExtents(width, height) method (followed by View.Camera.Apply), which also works just like zoom.  Or, you could simply switch back to your original command execution there.  Even command execution has a 'timing' related aspect to it, depending on how you do the Execute step (Execute, Execute2(False), Execute2(False)).

Maybe changing that part will change the timing issue, not sure. 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 7

blmartin
Participant
Participant

I appreciate you helping me with this. I tried adding pauses to the code to see if that would help, but it didn't.

System.Threading.Thread.CurrentThread.Sleep(1000)

 

Changing back to AppZoomAllCmd fixed the timing issue for zooming to extents. It still won't change the line weight in the exported image, yet I see the line bolden while it runs the code, and then goes back. Just doesn't get captured in the PNG.

 

Current code is below. I commented out the line type as the color and weight are the importants and with a thinner line, the dashes make the line hard to see.

 

Dim oInvApp As Inventor.Application = ThisApplication
Dim Doc As DrawingDocument = oInvApp.ActiveDocument
'get full path and file name, without file extension, from DrawingDocument object above
Dim sPathAndName As String = System.IO.Path.ChangeExtension(Doc.FullFileName, vbNullString)
Dim oASheet As Inventor.Sheet = Doc.ActiveSheet
Dim sSheetName As String = oASheet.Name
sSheetName = Replace(sSheetName, ":", "_")
Dim ImagePath As String = sPathAndName & "_" & sSheetName & ".png"
Dim oShtSettings As Inventor.SheetSettings = Doc.SheetSettings
Dim OriginalColor As Inventor.Color = oShtSettings.SheetColor
Dim oTO As TransientObjects = oInvApp.TransientObjects
Dim White As Inventor.Color = oTO.CreateColor(255, 255, 255)
Dim Red As Inventor.Color = oTO.CreateColor(255, 0, 0)
Dim Black As Inventor.Color = oTO.CreateColor(0, 0, 0)
Dim H1 As Double = oASheet.Height * 150
Dim W1 As Double = oASheet.Width * 150

Dim oLayer As Inventor.Layer = Doc.StylesManager.Layers.Item("Tweak Trail (ANSI)")
oLayer.Color = Red
ThisApplication.CommandManager.ControlDefinitions.Item("AppZoomAllCmd").Execute
Doc.SheetSettings.SheetColor = White
' oLayer.LineType = LineTypeEnum.kDoubleDashedChainLineType
oLayer.LineWeight = (2.54 / 1000) * 39

System.Threading.Thread.CurrentThread.Sleep(1000)

ThisApplication.ActiveView.SaveAsBitmap(ImagePath, W1, H1)

System.Threading.Thread.CurrentThread.Sleep(1000)

Doc.SheetSettings.SheetColor = OriginalColor
oShtSettings.SheetColor = OriginalColor
oLayer.Color = Black
' oLayer.LineType = LineTypeEnum.kContinuousLineType
oLayer.LineWeight = (2.54/1000) * 10 

 

0 Likes
Message 6 of 7

blmartin
Participant
Participant

Is there something in the image export that's making it ignore line weights, like exporting to PDF does? It's not an option at export, and I can't find anything online, but that's what it seems like it's doing.

0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor

Hi @blmartin.  Often the Thread Sleep route is not very effective, because it actually stops the processing of the current thread, which you really need to keep doing its thing.  One alternative that sometimes works is the UserInterfaceManager.DoEvents method.  This is sometimes used in a loop that is checking if some task has been finished, or if a property value has changed.  It is sort of a way to allow 'other stuff' that was instructed to happen up to that point to finish up before doing the next thing.  Not sure if it will work that way in this specific situation or not, but worth a try.  The other version of that method (View.SaveAsBitmapWithOptions) only gives you the additional option to get a transparent background.  That may not fit your needs, but if so, may take just enough extra time to process the image...not sure.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes