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

Modifying Viewport AlignDirection and AlignUp

werner_richtsfeld
Explorer

Modifying Viewport AlignDirection and AlignUp

werner_richtsfeld
Explorer
Explorer

Hello,

I need to modify some settings for a nwd file.
First I tried to set AlignDirection, AlignUp and Position of a viewport.
Changing any of these properties results in a "System.NotSupportedException: 'Object is Read-Only'" error

 

Do I have to copy the current active one, modify the values and replace the existing one?

 

Attached is the error which is shown in Visual Studio

 

Here is my code

namespace NavisworksPlugin
{
    [PluginAttribute("TestPlugin",                   //Plugin name
                    "ADSK",                                       //4 character Developer ID or GUID
                    ToolTip = "TestPlugin tool tip",//The tooltip for the item in the ribbon
                    DisplayName = "TestPlugin")]          //Display name for the Plugin in the Ribbon

    public class TestPlugin : AddInPlugin                        //Derives from AddInPlugin
    {
        public override int Execute(params string[] parameters)
        {
            return SetOrientation(100, 200, 300, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);
        }

        public int SetOrientation(double x, double y, double z, double forwardX, double forwardY, double forwardZ, double upX, double upY, double upZ)
        {
            try
            {
                Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
                Viewpoint viewpoint = doc.CurrentViewpoint.Value;
                viewpoint.Position = new Point3D(x, y, z);
                viewpoint.AlignDirection(new Vector3D(forwardX, forwardY, forwardZ));
                viewpoint.AlignUp(new Vector3D(upX, upY, upZ));
            }
            catch (Exception ex)
            {
                ShowMessageBox(ex.Message);
            }
            return 0;
        }
       
        private int ShowMessageBox(string message)
        {
          MessageBox.Show(Autodesk.Navisworks.Api.Application.Gui.MainWindow, message);
            return 0;
        }
    }
}

 

0 Likes
Reply
740 Views
10 Replies
Replies (10)

tetsuya_miwa
Advocate
Advocate

You cannot manipulate currentviewpoint directly.
Use .CreateCopy  then manipulate copied view then Do .CopyFrom.

0 Likes

werner_richtsfeld
Explorer
Explorer

Thank you for the update.

I managed to fix this, but I cant figure out how to set the orientation correct.
I would like to set to the default orientation (target) with my code, but there is a difference when it is done in the UI or via code.

public override int Execute(params string[] parameters)
{
    return SetOrientation(0, 0, 100, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);
}
       
public int SetOrientation(double x, double y, double z, double forwardX, double forwardY, double forwardZ, double upX, double upY, double upZ)
{
    try
    {
        Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
        Viewpoint viewpointCopy = doc.CurrentViewpoint.CreateCopy();

        //viewpointCopy.Position = new Point3D(x, y, z);
        viewpointCopy.AlignDirection(new UnitVector3D(forwardX, forwardY, forwardZ));
        viewpointCopy.AlignUp(new UnitVector3D(upX, upY, upZ));
        viewpointCopy.WorldUpVector = new UnitVector3D(upX, upY, upZ);

        Color SkyBackgroundColor = new Color(0.412, 0.549, 0.902);
        Color HorizonBackgroundColor = new Color(0.824, 0.941, 1);
        Color HorizonGroundBackgroundColor = new Color(0.47, 0.47, 0.47);
        Color GroundBackgroundColor = new Color(0.86, 0.86, 0.86);
       
        doc.SetHorizonBackground(SkyBackgroundColor, HorizonBackgroundColor, HorizonGroundBackgroundColor, GroundBackgroundColor);
        doc.CurrentViewpoint.CopyFrom(viewpointCopy);
    }
    catch (Exception ex)
    {
        ShowMessageBox(ex.Message);
    }
    return 0;
}

 

0 Likes

tetsuya_miwa
Advocate
Advocate

Target can be set with .PointAt.

0 Likes

werner_richtsfeld
Explorer
Explorer

Could you point mi once more in the right direction.
When copying the viewpoint and modifying the AlignDirection And AlignUp which I assume are the Orientation North and Orientation Up, these values wont change like they do when pressing the Defaults button in the dialog.

How can I copy this Default button press action?
Which properties are affected by this?


0 Likes

tetsuya_miwa
Advocate
Advocate

I don't understand which default button in what dialog are you talking about?

0 Likes

werner_richtsfeld
Explorer
Explorer

This on in Ribbon Menu Home-> File Options -> Orientations Tab and then below the coordinates.
Using these methods / properties does not have the same outcome as pressing the defaults button

 

 

viewpointCopy.AlignDirection(new UnitVector3D(forwardX, forwardY, forwardZ));
viewpointCopy.AlignUp(new UnitVector3D(upX, upY, upZ));
viewpointCopy.WorldUpVector = new UnitVector3D(upX, upY, upZ);

 

  

0 Likes

tetsuya_miwa
Advocate
Advocate

You want to change definition of north and up of current file instead of changing current view?

 

0 Likes

werner_richtsfeld
Explorer
Explorer

Yes, my task is (I'm no Navisworks user) to setup the orientation like that in the dialog's default and set the viewpoint to Viewpoint-> Set Viewpoint Up -> Set Up +Z and change the Background to Horizon (which is fine already)


0 Likes

tetsuya_miwa
Advocate
Advocate
0 Likes

werner_richtsfeld
Explorer
Explorer

Hello,

I wanted to follow up on this answer, because maybe I understood the terms wrong.

Is setting the target with ".PointAt" the same as selecting the "Set Up +Z" from the attached screenshot?

Is the Position the same as the value which is set by PointAt?

Which values need to be set to get the +Z viewpoint?

Is it (0,0,1) or (position.X, position.Y, 1)

0 Likes