In the last post, we introduce the objects of viewpoint in .NET API. The API also allows you to manipulate the viewpoint. In general, we will need to manipulate current viewpoint. The current viewpoint is managed by the document part CurrentViewpoint. Same to other document part, we cannot modify it directly. The workflow is:
1) get the current viewpoint
2) create a copy from the current viewpoint
3) modify the properties of the copy
4) call CurrentViewpoint.CopyFrom with the copy
Following are some codes demos.
// change some general properties
privatevoid changeVPPro()
{
Document oDoc =
Autodesk.Navisworks.Api.Application.ActiveDocument;
// get current viewpoint
Viewpoint oCurVP = oDoc.CurrentViewpoint;
// get copy viewpoint
Viewpoint oCopyVP = oCurVP.CreateCopy();
oCopyVP.FarPlaneDistance *= 2;
oCopyVP.NearPlaneDistance *= 2;
if (oCopyVP.Tool != Tool.NavigateWalk)
oCopyVP.Tool = Tool.NavigateWalk;
oCopyVP.LinearSpeed *= 2;
oCopyVP.AngularSpeed *= 2;
if (oCopyVP.Projection != ViewpointProjection.Orthographic)
oCopyVP.Projection = ViewpointProjection.Orthographic;
if (oCopyVP.RenderStyle != ViewpointRenderStyle.Wireframe)
oCopyVP.RenderStyle = ViewpointRenderStyle.Wireframe;
//update the current viewpoint
oDoc.CurrentViewpoint.CopyFrom(oCopyVP);
}
// move position of the camera 2 distance along X axis
privatevoid moveCameraPos()
{
Document oDoc =
Autodesk.Navisworks.Api.Application.ActiveDocument;
// make a copy of current viewpoint
Viewpoint oCurrVCopy = oDoc.CurrentViewpoint.CreateCopy();
//step to move
double step = 2;
// create the new position
Point3D newPos =
newPoint3D(oCurrVCopy.Position.X + step,
oCurrVCopy.Position.Y,
oCurrVCopy.Position.Z);
oCurrVCopy.Position = newPos;
// update current viewpoint
oDoc.CurrentViewpoint.CopyFrom(oCurrVCopy);
}
// move position of the camera 2 along view direction
privatevoid moveCameraAlongViewDir()
{
Document oDoc =
Autodesk.Navisworks.Api.Application.ActiveDocument;
// make a copy of current viewpoint
Viewpoint oCurrVCopy = oDoc.CurrentViewpoint.CreateCopy();
// get view direction
Vector3D oViewDir = getViewDir(oCurrVCopy);
//step to move
double step = 2;
// create the new position
Point3D newPos =
newPoint3D(oCurrVCopy.Position.X + oViewDir.X * step,
oCurrVCopy.Position.Y + oViewDir.Y * step,
oCurrVCopy.Position.Z + oViewDir.Z * step);
oCurrVCopy.Position = newPos;
// update current viewpoint
oDoc.CurrentViewpoint.CopyFrom(oCurrVCopy);
}
// change view direction
// set PointAt
privatevoid changeCameraViewDir_Way1()
{
Document oDoc =
Autodesk.Navisworks.Api.Application.ActiveDocument;
// make a copy of current viewpoint
Viewpoint oCurrVCopy = oDoc.CurrentViewpoint.CreateCopy();
// Focal Distance
double oFocal =
oCurrVCopy.FocalDistance;
Point3D oPos = oCurrVCopy.Position;
// get current view direction
Vector3D oViewDir = getViewDir(oCurrVCopy);
//current target point (Loot At)
Point3D oTarget =
newPoint3D(oPos.X + oViewDir.X * oFocal,
oPos.Y + oViewDir.Y * oFocal,
oPos.Z + oViewDir.Z * oFocal);
//step to move
double step = 2;
// new Point At
Point3D newPointAt = newPoint3D(oTarget.X + step,
oTarget.Y + step,
oTarget.Z + step);
// set to new target
oCurrVCopy.PointAt(newPointAt );
// Note: when you set Point At, the position and focal
// do not change, while target (Look At) changes, i.e.
// view direction changes.
// update current viewpoint
oDoc.CurrentViewpoint.CopyFrom(oCurrVCopy);
}
//change view direction directly.
//note: will require combination of Position, Target and AlignUp
// in this demo: implement LEFT and RIGHT view
privatevoid changeCameraViewDir_Way2()
{
Document oDoc =
Autodesk.Navisworks.Api.Application.ActiveDocument;
// make a copy of current viewpoint
Viewpoint oCurrVCopy = oDoc.CurrentViewpoint.CreateCopy();
// Focal Distance
double oFocal =
oCurrVCopy.FocalDistance;
// new target is the center of the model
Point3D oNewTarget = oDoc.Models[0].RootItem.BoundingBox().Center;
//new direction is X- >> RIGHT
Vector3D oNewViewDir = newVector3D(-1, 0, 0);
//new direction is X >> LEFT
//Vector3D oNewViewDir = new Vector3D(1, 0, 0);
//calculate the new position by the target and focal distance
Point3D oNewPos = newPoint3D(oNewTarget.X - oNewViewDir.X * oFocal,
oNewTarget.Y - oNewViewDir.Y * oFocal,
oNewTarget.Z - oNewViewDir.Z * oFocal);
//set the position
oCurrVCopy.Position = oNewPos;
//set the target
oCurrVCopy.PointAt(oNewTarget);
//set view direction
oCurrVCopy.AlignDirection(oNewViewDir);
// set which direction is up: in this case it is Z+
oCurrVCopy.AlignUp(newVector3D(0, 0, 1));
// update current viewpoint
oDoc.CurrentViewpoint.CopyFrom(oCurrVCopy);
}
// help function: get view direction
privateVector3D getViewDir(Viewpoint oVP)
{
Rotation3D oRot = oVP.Rotation;
// calculate view direction
Rotation3D oNegtiveZ =
newRotation3D(0, 0, -1, 0);
Rotation3D otempRot =
MultiplyRotation3D(oNegtiveZ, oRot.Invert());
Rotation3D oViewDirRot =
MultiplyRotation3D(oRot, otempRot);
// get view direction
Vector3D oViewDir =
newVector3D(oViewDirRot.A,
oViewDirRot.B,
oViewDirRot.C);
oViewDir.Normalize();
returnnewVector3D(oViewDir.X,
oViewDir.Y,
oViewDir.Z);
}
// help function: Multiply two Rotation3D
privateRotation3D MultiplyRotation3D(
Rotation3D r2,
Rotation3D r1)
{
Rotation3D oRot =
newRotation3D(r2.D * r1.A + r2.A * r1.D +
r2.B * r1.C - r2.C * r1.B,
r2.D * r1.B + r2.B * r1.D +
r2.C * r1.A - r2.A * r1.C,
r2.D * r1.C + r2.C * r1.D +
r2.A * r1.B - r2.B * r1.A,
r2.D * r1.D - r2.A * r1.A -
r2.B * r1.B - r2.C * r1.C);
oRot.Normalize();
return oRot;
}
In the next post, we will demo a few more codes to align the up vector of the camera and rotate camera.
(To be continued)