Preview Window in AutoCAD using .NET API

Preview Window in AutoCAD using .NET API

Anonymous
Not applicable
7,186 Views
17 Replies
Message 1 of 18

Preview Window in AutoCAD using .NET API

Anonymous
Not applicable
Hi everyone, I want to build the application such like that it shows preview in the GUI (User Interface) before it draw anything in the AutoCAD. Note that I want to create 3D objects in AutoCAD and if possible then preview window should be allow user to rotate the 3D objects using mouse in preview window. I hope this much is enough to understand my requirement. Thanks in advance.
0 Likes
7,187 Views
17 Replies
Replies (17)
Message 2 of 18

FRFR1426
Collaborator
Collaborator

You can render to a picture box this way:

 

Manager gsm = doc.GraphicsManager;

using (var view = new View())
{
var cvport = (short)Application.GetSystemVariable("CVPORT"); doc.GraphicsManager.SetViewFromViewport(view, cvport); view.SetView(new Point3d(0, 0, 1), Point3d.Origin, Vector3d.YAxis, pictureBox.Width, pictureBox.Height); var descriptor = new KernelDescriptor(); descriptor.addRequirement(Autodesk.AutoCAD.UniqueString.Intern("3D Drawing")); GraphicsKernel kernel = Manager.AcquireGraphicsKernel(descriptor); Device dev = gsm.CreateAutoCADOffScreenDevice(kernel); using (dev) { dev.OnSize(pictureBox.Size); dev.DeviceRenderType = RendererType.Default; dev.BackgroundColor = Color.White; dev.Add(view); dev.Update(); try { using (Model model = gsm.CreateAutoCADModel(kernel)) { var extents3D = new Extents3d(); var line = new Line(p1, p2); [..] view.Add(line, model); extents3D.AddExtents(line.Bounds.Value); view.ZoomExtents(extents3D.MinPoint, extents3D.MaxPoint); view.Zoom(0.9); } pictureBox.Image = view.GetSnapshot(new Rectangle(0, 0, pictureBox.Width - 1, pictureBox.Height - 1)); finally { view.EraseAll(); dev.Erase(view); if (null != line) line.Dispose(); } } }
Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 3 of 18

Anonymous
Not applicable
Thanks for your help...! Just one question will I be able to rotate objects in picture box through Mouse?
0 Likes
Message 4 of 18

FRFR1426
Collaborator
Collaborator

No this code is for a static preview. To get orbit functionality, you'll need to write a custom control which will handle the mouse movements and call view.Orbit to update the view.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 5 of 18

dgorsman
Consultant
Consultant

A slightly sideways but less development-intensive option may be to generate a temporary 3D DWF and then pass it to a separate Design Review or Navisworks Freedom session; there might also be the option to embed a Navisworks viewer object.  All the navigation tools, none of the labor.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 6 of 18

FRFR1426
Collaborator
Collaborator

http://through-the-interface.typepad.com/through_the_interface/2008/06/autocad-net-ver.html

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 7 of 18

FRFR1426
Collaborator
Collaborator

Here is a complete control:

 

using System;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.AutoCAD;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsSystem;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
using View = Autodesk.AutoCAD.GraphicsSystem.View;

namespace TestCS
{
    public partial class PreviewUserControl : UserControl
    {
        #region Fields

        Device device;

        Extents3d extents;

        Model model;

        Solid3d solid;

        Point start;

        View view;

        #endregion

        #region Constructors

        public PreviewUserControl()
        {
            InitializeComponent();

            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
        }

        #endregion

        #region Methods

        void CleanUp()
        {
            if (solid != null)
            {
                solid.Dispose();
                solid = null;
            }

            if (model != null)
            {
                model.Dispose();
                model = null;
            }

            if (view != null)
            {
                // View need to be removed from device
                device?.EraseAll();

                view.Dispose();
                view = null;
            }

            if (device != null)
            {
                device.Dispose();
                device = null;
            }
        }

        protected override void CreateHandle()
        {
            base.CreateHandle();

            if (DesignMode) return;

            Document doc = Application.DocumentManager.MdiActiveDocument;
            Manager gsm = doc.GraphicsManager;

            var descriptor = new KernelDescriptor();
            descriptor.addRequirement(UniqueString.Intern("3D Drawing"));
            GraphicsKernel kernel = Manager.AcquireGraphicsKernel(descriptor);
            device = gsm.CreateAutoCADDevice(kernel, Handle);
            device.OnSize(Size);

            view = new View();
            view.SetView(new Point3d(1, 1, 1), view.Target, view.UpVector, view.FieldWidth, view.FieldHeight);
            device.Add(view);

            model = gsm.CreateAutoCADModel(kernel);
            extents = new Extents3d();

            solid = new Solid3d();
            solid.CreateBox(1, 1, 1);
            solid.ColorIndex = 1;
            view.Add(solid, model);
            if (solid.Bounds != null) extents.AddExtents(solid.Bounds.Value);

            HandleDestroyed += PreviewUserControl_HandleDestroyed;
        }

        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            view.ZoomExtents(extents.MinPoint, extents.MaxPoint);
            RefreshView();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            start = e.Location;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.Button != MouseButtons.Left) return;
            view.Orbit(Math.PI * (start.X - e.X) / Width, Math.PI * (e.Y - start.Y) / Height);
            start = e.Location;
            RefreshView();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            RefreshView();
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            device?.OnSize(Size);
            RefreshView();
        }

        void PreviewUserControl_HandleDestroyed(object sender, EventArgs e)
        {
            CleanUp();
        }

        void RefreshView()
        {
            if (view == null) return;
            view.Invalidate();
            view.Update();
        }

        #endregion
    }
}

2015-09-16_201848.png

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 8 of 18

Anonymous
Not applicable
Hi, I am not able to build the Application due to KernelDescriptor. var descriptor = new KernelDescriptor(); 1. May I know which references I have to add for KernelDescriptor? Is this class belonging to AutoCAD APIs or Microsoft(Visual Studio) APIs ? 2. This code is for AutoCAD .NET API not for ObjectARX. Am I Right ?
0 Likes
Message 9 of 18

FRFR1426
Collaborator
Collaborator

KernelDescriptor is for AutoCAD 2015 and more. Before that, you only need to pass the window handle to the CreateAutoCADDevice method:

 

device = gsm.CreateAutoCADDevice(Handle);

1. KernelDescriptor is in the Autodesk.AutoCAD.GraphicsSystem namespace (Acdbmgd.dll). It's an AutoCAD API.

2. It's for .NET API, not for ObjectARX (C++) API

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 10 of 18

Anonymous
Not applicable
Bonjour, Je viens d'utiliser votre exemple (super au passage), et j'aimerai après l'orbite pouvoir revenir à une vue de dessus... sans succès. :(( Cordialement.
0 Likes
Message 11 of 18

FRFR1426
Collaborator
Collaborator

Pour les vues standards (ortho/isométrique), il faut utiliser la méthode SetView qui permet de repositionner la caméra. Pour une vue de dessus (vue du plan XY), ça donne ça :

 

view.SetView(view.Target + Vector3d.ZAxis, view.Target, Vector3d.YAxis, view.FieldWidth, view.FieldHeight);

On conserve la cible et les dimensions du champ de vue, on positionne la caméra juste au dessus de la cible et on réaligne le haut de la caméra avec l'axe Y.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 12 of 18

Anonymous
Not applicable

Merci pour votre réponse.

 

Cordialement.

 

Patrick.

0 Likes
Message 13 of 18

Anonymous
Not applicable

捕获.PNG

 

Do I lack of some namespace or class KernelDescriptor is a new one?

0 Likes
Message 14 of 18

norman.yuan
Mentor
Mentor

If you read through the thread carefully, you'd have seen it:

 

FRFR1426 answered it on 09-16-2015: The name space is Autodesk.AutoCAD.GraphicsSystem (in acdbmgd.dll).

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 15 of 18

kerry_w_brown
Advisor
Advisor

 @Anonymous

 

I think Norman means Post 9 of 14

 

The dates displayed are expressed in local time and may differ for readers in differing locations.

 

Regards,

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 16 of 18

Anonymous
Not applicable

Hello,
How do you incorporate 'LineWeight' on this?
I wanted to adapt this on our app's preview window, and I wanted to add a feature where an object appears to be highlighted (in a sense that the line weight would appear bolder) on the snapshot.

0 Likes
Message 17 of 18

A_Taha
Participant
Participant
Thanks for the control, it works great. Is it possible to add zoom-in and zoom-out functionality with the mouse wheel?

Regards,
A. Taha
0 Likes
Message 18 of 18

_gile
Consultant
Consultant

Hi,

You can override the OnMouseWheel event handler.

        protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);
            if (0 < e.Delta)
                view.Zoom(1.6);
            else
                view.Zoom(0.625);
            RefreshView();
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes