.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Preview Window in AutoCAD using .NET API

17 REPLIES 17
Reply
Message 1 of 18
Anonymous
4989 Views, 17 Replies

Preview Window in AutoCAD using .NET API

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.
17 REPLIES 17
Message 2 of 18
FRFR1426
in reply to: Anonymous

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
Message 3 of 18
Anonymous
in reply to: FRFR1426

Thanks for your help...! Just one question will I be able to rotate objects in picture box through Mouse?
Message 4 of 18
FRFR1426
in reply to: Anonymous

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
Message 5 of 18
dgorsman
in reply to: FRFR1426

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.


Message 6 of 18
FRFR1426
in reply to: dgorsman

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
Message 7 of 18
FRFR1426
in reply to: FRFR1426

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
Message 8 of 18
Anonymous
in reply to: FRFR1426

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 ?
Message 9 of 18
FRFR1426
in reply to: Anonymous

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
Message 10 of 18
Anonymous
in reply to: FRFR1426

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.
Message 11 of 18
FRFR1426
in reply to: Anonymous

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
Message 12 of 18
Anonymous
in reply to: FRFR1426

Merci pour votre réponse.

 

Cordialement.

 

Patrick.

Message 13 of 18
Anonymous
in reply to: FRFR1426

捕获.PNG

 

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

Message 14 of 18
norman.yuan
in reply to: Anonymous

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

Message 15 of 18
kdub_nz
in reply to: Anonymous

 @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 in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 16 of 18
Anonymous
in reply to: kdub_nz

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.

Message 17 of 18
A_Taha
in reply to: FRFR1426

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
Message 18 of 18
_gile
in reply to: A_Taha

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Customer Advisory Groups


Autodesk Design & Make Report