How to open file by AutoCAD command

How to open file by AutoCAD command

Anonymous
Not applicable
1,890 Views
7 Replies
Message 1 of 8

How to open file by AutoCAD command

Anonymous
Not applicable

Hi! 

    I want open DWG file by AutoCAD command. There are two methods Application.DocumentManager.MdiActiveDocument.SendStringToExecute() and C++ acedCmd() implemented as follow: [DllImport("accore.dll", EntryPoint = "acedCmd", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)] private extern static int acedCmd(IntPtr rbp);

    But i don't know how to write the correct code.

    I have tried the following code, but ITdoes not work.

   (1) ResultBuffer rb = new ResultBuffer();
        rb.Add(new TypedValue(5005, "_.OPEN D:\\1.dwg"));
        if (!AcadApp.DocumentManager.IsApplicationContext)
              acedCmd(rb.UnmanagedObject);

   (2)AcadApp.DocumentManager.MdiActiveDocument.SendStringToExecute("OPEN D:\\1.dwg ", true, false, true);

   Anybody can help me.

0 Likes
Accepted solutions (1)
1,891 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant

Hi

 

You do not need to call the OPEN command to open a drawing.

 

You should use the DocumentCollection.Open() method.

 

DocumentCollection docMgr = AcadApp.DocumentManager;
Document doc = docMgr.Open("D:\\1.dwg");
docMgr.MdiActiveDocument = doc;


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 8

Anonymous
Not applicable

Thank you! I did as you said.

I have a problem about using WPF inside an AutoCAD palette, and i think the problem may be solved if i use another way to open file.

So, my question is whether there is another way to open DWG file. 

0 Likes
Message 4 of 8

_gile
Consultant
Consultant

The upper method should work from a WPF click event:

 

 

        private void CmdButton_Click(object sender, RoutedEventArgs e)
        {
            DocumentCollection docMgr = AcAp.DocumentManager;
            docMgr.Open("D:\\1.dwg", false);
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 8

_gile
Consultant
Consultant
Accepted solution

Here's a minimalist example of PaletteSet with a WPF UserControl.

 

Commands class

using Autodesk.AutoCAD.Runtime;

namespace OpenDwgFromWpfPalette
{
    public class Commands
    {
        static CustomPaletteSet palette;

        [CommandMethod("TEST_PALETTE")]
        public void CmdPalette()
        {
            if (palette == null)
                palette = new CustomPaletteSet();
            palette.Visible = true;
        }
    }
}

 

 

CustomPaletteSet class

using Autodesk.AutoCAD.Windows;

namespace OpenDwgFromWpfPalette
{
    public class CustomPaletteSet : PaletteSet
    {

        public CustomPaletteSet() : base("Palette")
        {
            AddVisual("Open file", new PaletteTab());
        }
    }
}

 

PaletteTab XAML

<UserControl x:Class="OpenDwgFromWpfPalette.PaletteTab"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:OpenDwgFromWpfPalette"
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="250">
    <StackPanel Background="WhiteSmoke">
        <Button x:Name="OpenButton" Width="80" Margin="10" Click="OpenButton_Click">Open</Button>
    </StackPanel>
</UserControl>

 

PaletteTab code behind

using Autodesk.AutoCAD.ApplicationServices;
using System.Windows;
using System.Windows.Controls;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;

namespace OpenDwgFromWpfPalette
{
    public partial class PaletteTab : UserControl
    {
        public PaletteTab()
        {
            InitializeComponent();
        }

        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
            AcAp.DocumentManager.Open("D:\\1.dwg", false);
        }
    }
} 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 8

Anonymous
Not applicable

Thank you!

0 Likes
Message 7 of 8

Anonymous
Not applicable

Did you run the code in CAD2008? 

0 Likes
Message 8 of 8

_gile
Consultant
Consultant

@Anonymous wrote:

Did you run the code in CAD2008? 


No, only 2013 to 2018.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes