Changing Background for publishing file using Script Automation in Navisworks

By Saikat Bhattacharya

One of the SDK samples in Navisworks called AutoSimpleScriptExample shows how to use the Navisworks API with the VB script file. If there is a requirement to change the background color from black to another color, say, red during this automation process, how can we achieve this?

All available attributes for publishing are listed in InwOaPublishAttribute. There is no attribute to control the background color. However, you can use state.BackgroundColor before publishing. Performing the following changes (highlighted in grey) to the VB script file included in the AutoSimpleScriptExample, helps change the background color to Red.

 

' Navisworks API – AutoSimpleScriptExample – Script Automation

' Example of using the Navisworks API with VB script file

'————————————————————

 

'TODO: The filename needs setting to a suitable .nwd file.

 

'create new document

set navis_doc=CreateObject("Navisworks.Document"

 

'make sure it's visible 

navis_doc.visible=true 

 

'open document

navis_doc.OpenFile("C:\Program Files\Autodesk\Navisworks Manage 2013\API\COM\examples\gatehouse.nwd")

 

'set current view to first saved view

navis_doc.state.currentview=navis_doc.state.savedviews(1).anonview

 

dim ndx

ndx = navis_doc.state.getenum("eObjectType_nwLVec3f")

set color =navis_doc.state.objectFactory(ndx)

color.data1 = 1

color.data2 = 0

color.data3 = 0

navis_doc.state.BackgroundColor = color

 

'make sure app stays open with no refs

navis_doc.stayopen

Using the SDK sample as it is:

image

After modifying the code to change the color: image


Comments

4 responses to “Changing Background for publishing file using Script Automation in Navisworks”

  1. Dan Cisecki Avatar
    Dan Cisecki

    Hello, I am new to coding and I understand the concept of the example above and have used it to change the background color of multiple files use a batch routine. I would like to do the same thing with changing the object color of all the objects in the file instead of the background. Can you point me in the right direction? See below for the code I used in my batch file.
    ‘ChangeBackgroundColour.cmd
    cscript “ChangeBackgroundColour.vbs” “Source Directory” “Source NWD File” “Destination Directory”
    ‘ChangeBackgroundColour.vbs
    If WScript.Arguments.Count > 2 Then
    strOriginalFileFolder = WScript.Arguments(0)
    strOriginalFileName = WScript.Arguments(1)
    strNewFileFolder = WScript.Arguments(2)
    strNewFileName = strOriginalFileName
    Else
    MsgBox “Field missing! Syntax = Original Folder, Original File, Destination Folder”
    WScript.Quit
    End If
    ‘create new document
    set navis_doc=CreateObject(“NavisWorks.Document”)
    ‘make sure it’s not visible
    navis_doc.visible=false
    ‘open original document
    navis_doc.OpenFile(strOriginalFileFolder & strOriginalFileName)
    dim ndx
    ndx = navis_doc.state.getenum(“eObjectType_nwLVec3f”)
    set color =navis_doc.state.objectFactory(ndx)
    ‘Set background color by variant of RGB color
    ‘red = 1,0,0
    ‘green = 0,1,0
    ‘blue = 0,0,1
    ‘black = 0,0,0
    ‘white = 255,255,255
    ‘0 = black; 1 = highest number for color; 255 is lowest.
    ‘Set color to white
    color.data1 = 255
    color.data2 = 255
    color.data3 = 255
    ‘background color set to color
    navis_doc.state.BackgroundColor = color
    ‘Do not want to overwrite existing attributes so use saveas
    ‘ndx=navis_doc.state.getenum(“eObjectType_nwOaPublishAttribute”) ‘publish with attributes
    ‘set attrib=navis_doc.state.objectFactory(ndx)
    ‘navis_doc.publishfile strNewFileFolder & strNewFileName,attrib
    navis_doc.saveas strNewFileFolder & strNewFileName

  2. Dear Saikat,
    Could you please let me know where should I put this code in Navis?
    I have found the example in the SDK but I don’t know how to proceed.
    Thank you in advance
    Hamid

  3. Hello,
    Is there a way to change the Navisworks background mode to Graduated and Horizon in VBScirpt or in VB.Net?

  4. how to create a batch it should automatically change the colour of total NWD file and its transparency also

Leave a Reply to HamidCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading