3DWalk through an AutoCAD Model

By Balaji Ramamoorthy

Here is a sample code to demonstrate the navigation through an AutoCAD model. In this sample code, the camera and target positions follow the selected spline entity which defines the navigation path.

Here is a recording of the output :
 

Here is the sample code :

[CommandMethod("MyWalk")]
public static void MyWalkMethod()
{
    Document doc
                = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;
 
    PromptEntityOptions peo2
                = new PromptEntityOptions("nSelect a spline :");
    peo2.SetRejectMessage(
        "Please select a spline for defining the 3D walk path");
    peo2.AddAllowedClass(
        typeof(Autodesk.AutoCAD.DatabaseServices.Spline), false);
    PromptEntityResult per2 = ed.GetEntity(peo2);
    if (per2.Status != PromptStatus.OK)
        return;
    ObjectId splineId = per2.ObjectId;
 
    int steps = 3000;
 
    Point3d position = Point3d.Origin;
    Point3d target = Point3d.Origin;
 
    // Create and add our message filter to know if
    // escape is pressed during walk.
    MyMessageFilter filter = new MyMessageFilter();
    System.Windows.Forms.Application.AddMessageFilter(filter);
 
    ViewportTableRecord backupVTR = new ViewportTableRecord();
 
    const double DIAG35MM = 42.0;
    double lensLength = 50.0;
    double projectionPlaneDistance = 50.0;
    double sp = 0.0;
    double ep = 0.0;
    double aspectRatio = 1.0;
 
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        ViewTable viewTable = tr.GetObject(
                                            db.ViewTableId,
                                            OpenMode.ForWrite
                                            ) as ViewTable;
 
        ViewportTable vpTbl = tr.GetObject(
                                            db.ViewportTableId,
                                            OpenMode.ForRead
                                            ) as ViewportTable;
 
        ViewportTableRecord viewportTableRec = tr.GetObject
                                    (
                                            vpTbl["*Active"],
                                            OpenMode.ForRead
                                    ) as ViewportTableRecord;
 
        backupVTR.CopyFrom(viewportTableRec);
        aspectRatio =
            (viewportTableRec.Width / viewportTableRec.Height);
 
        Spline spline = tr.GetObject(
                                        splineId,
                                        OpenMode.ForWrite
                                    ) as Spline;
        sp = spline.StartParam;
        ep = spline.EndParam;
        spline.Visible = false;
 
        tr.Commit();
    }
 
    using (Transaction tr
                    = db.TransactionManager.StartTransaction())
    {
        Spline spline = tr.GetObject(
                                        splineId,
                                        OpenMode.ForRead
                                    ) as Spline;
 
        double paramincr = (ep - sp) / steps;
 
        for (double param = sp; param

For the recording and testing of the sample code, I downloaded a nice sample drawing of a little house from here and made some minor changes to make it to more realistic. The modified sample drawing can be downloaded here : 

Download House.dwg

To try the sample code, build the sample code and netload in AutoCAD. Open the sample drawing and run “MyWalk” command. Select the yellow spline when prompted for the 3D walk path. During the walk, press escape key to stop the navigation if you need to stop it.


Comments

4 responses to “3DWalk through an AutoCAD Model”

  1. This is a nice suggestion. Could you increase the number of steps and decrease the sleep time in order to get a smoother animation?

  2. Hi Erik,
    Thanks for your comment. I have updated the sample code and the recording. The animation is much smoother than what it was earlier.
    Regards,
    Balaji

  3. BJHuffine Avatar
    BJHuffine

    Curious… now that RenderMode is gone, how would you handle that part?

  4. Hi Jason,
    In AutoCAD 2015, instead of the RenderMode, you can set vtr.VisualStyleId. But for the drawing that is attached to this post,
    simply commenting out the vtr.RenderMode should also work ok.
    Regards,
    Balaji

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading