.NET

.NET

Reply
Active Member
lfecchio
Posts: 7
Registered: ‎10-19-2012
Message 1 of 6 (1,249 Views)

Troubles in a C# interop to access a ActiveLayout.PlotType

1249 Views, 5 Replies
10-19-2012 08:49 AM

Hi!

 

I doesen't acces or modify nothing in a ActiveLayout.

I'm using c# and interop and need to plot to a file a dwg.

The plot to file works well, but I need to change some properties like Plot Scale or Plot Type.

 

A conversion error occours.

 

In second place, the DefaultPlotStyleTable doesen't work too.

 

Somebody can helpe me?

 

My test code:

 

using System;
using System.IO;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;

namespace ProcessoCAD
{
   static class Program
   {
      private static Autodesk.AutoCAD.Interop.AcadApplication oAcadApp = null;
      private static string sAcadID = "AutoCAD.Application.18";

      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main()
      {
         // Abre AutoCAD
         try
         {
            oAcadApp = (Autodesk.AutoCAD.Interop.AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject(sAcadID);
         }
         catch
         {
            System.Type AcadProg = System.Type.GetTypeFromProgID(sAcadID);
            oAcadApp = (Autodesk.AutoCAD.Interop.AcadApplication)System.Activator.CreateInstance(AcadProg);
         }

         // Inicia ciclo de execução
         if (oAcadApp != null)
         {
            string StyleTable = "AZEVEDO.ctb";

            oAcadApp.Visible = true; //could leave this false to hide Acad from the user

            Autodesk.AutoCAD.Interop.AcadDocument dm; // = oAcadApp.ActiveDocument;
            
            dm = oAcadApp.Documents.Open("d:\\teste\\Teste.dwg", true, null);
            dm.SetVariable("BACKGROUNDPLOT", 0);
            dm.ActiveSpace = AcActiveSpace.acPaperSpace;
            dm.ActiveLayout.PlotType = AcPlotType.acExtents;
            dm.ActiveLayout.StandardScale = AcPlotScale.ac1_1;

            oAcadApp.Preferences.Output.DefaultPlotStyleTable = StyleTable;
            dm.Regen(AcRegenType.acActiveViewport);

            // dm.Application.ZoomExtents();
            // dm.Regen(AcRegenType.acAllViewports);
            dm.Plot.QuietErrorMode = true;
            dm.Plot.NumberOfCopies = 1;
            dm.Plot.PlotToFile("d:\\teste\\Teste.pdf", "C:\\Program Files\\AutoCAD 2010\\UserDataCache\\Plotters\\DWG To PDF.pc3");
//          dm.Plot.PlotToFile("d:\\teste\\Teste.plt", "C:\\Program Files\\AutoCAD 2010\\UserDataCache\\Plotters\\DWF6 ePlot.pc3");
            
            //dm.Close(null, null);

            //oAcadApp.Quit();
         }
      }
    }
}

 

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 6 (1,217 Views)

Re: Troubles in a C# interop to access a ActiveLayout.PlotType

10-20-2012 03:50 AM in reply to: lfecchio

Try this code snip, change to your settins and file name:

 

           ' Abre AutoCAD
            Try
                oAcadApp = DirectCast(System.Runtime.InteropServices.Marshal.GetActiveObject(sAcadID), Autodesk.AutoCAD.Interop.AcadApplication)
            Catch
                Dim AcadProg As System.Type = System.Type.GetTypeFromProgID(sAcadID)
                oAcadApp = DirectCast(System.Activator.CreateInstance(AcadProg), Autodesk.AutoCAD.Interop.AcadApplication)
            End Try
            oAcadApp.Visible = True
            Try
                ' Inicia ciclo de execução
                If oAcadApp IsNot Nothing Then
                    Dim StyleTable As String = "MyPlotStyle.ctb"
                    ' set application to be visible
                    oAcadApp.Visible = True
                    ' expand window to max
                    oAcadApp.WindowState = AcWindowState.acMax
                    'could leave this false to hide Acad from the user
                    Dim dm As Autodesk.AutoCAD.Interop.AcadDocument
                    ' = oAcadApp.ActiveDocument;
                    dm = oAcadApp.Documents.Open("c:\test\layouts.dwg", True, Nothing)
                    ' set desired layouts first
                    dm.ActiveLayout = dm.Layouts.Item("FLOOR PLAN")
                    dm.SetVariable("BACKGROUNDPLOT", 0)
                    dm.ActiveSpace = AcActiveSpace.acPaperSpace
                    dm.ActiveLayout.PlotType = AcPlotType.acExtents
                    dm.ActiveLayout.StandardScale = AcPlotScale.ac1_1
                    dm.Utility.Prompt(vbLf & oAcadApp.Preferences.Output.DefaultPlotStyleTable)
                    'MsgBox(dm.ActiveLayout.GetPlotStyleTableNames(0).ToString)
                    Dim pstyle As String
                    Dim pstyles() As String = TryCast(dm.ActiveLayout.GetPlotStyleTableNames, String())
                    For Each pstyle In pstyles
                        If String.Compare(StyleTable, StringComparison.InvariantCulture) = 0 Then
                            oAcadApp.Preferences.Output.DefaultPlotStyleTable = StyleTable
                        Else
                            oAcadApp.Preferences.Output.DefaultPlotStyleTable = "acad.stb"
                        End If
                    Next

                    dm.Regen(AcRegenType.acActiveViewport)

                    ' dm.Application.ZoomExtents();
                    ' dm.Regen(AcRegenType.acAllViewports);
                    dm.Plot.QuietErrorMode = True
                    dm.Plot.NumberOfCopies = 1
                    '          dm.Plot.PlotToFile("d:\\teste\\Teste.plt", "C:\\Program Files\\AutoCAD 2010\\UserDataCache\\Plotters\\DWF6 ePlot.pc3");

                    'dm.Close(null, null);

                    'oAcadApp.Quit();
                    dm.Plot.PlotToFile("c:\test\layouts.pdf", "C:\Program Files\AutoCAD 2009\UserDataCache\Plotters\DWG To PDF.pc3")
                End If
            Catch ex As System.Exception
                MsgBox(ex.Message & vbLf & ex.StackTrace)

            End Try
        End Sub

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Member
lfecchio
Posts: 7
Registered: ‎10-19-2012
Message 3 of 6 (1,183 Views)

Re: Troubles in a C# interop to access a ActiveLayout.PlotType

10-24-2012 04:34 AM in reply to: lfecchio

Thanks for help!

I converted the code to VB, but when it executes the line

 

dm.ActiveLayout = dm.Layouts.Item(0)

 

I have a conversion error:

 

"Unable to cast COM object of type 'Autodesk.AutoCAD.Interop.Common.AcadLayoutsClass' interface type 'Autodesk.AutoCAD.Interop.Common.IAcadLayouts.' This operation failed because the QueryInterface call on the COM component for the interface with IID '{9AEBED7B-03A9-4C9E-B358-4E95FA5B07F4}' failed due to the following error: No support for this interface (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

 

Is it because my dwg or a wrong reference?

 

I'm send the dwg and need to use the AR2 Layout.

 

Thanks

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 6 (1,174 Views)

Re: Troubles in a C# interop to access a ActiveLayout.PlotType

10-24-2012 09:58 AM in reply to: lfecchio

Sorry for the belating I was busy

I tested code I posted above on my machine -

 the plotting is looks good,

See plot copy log:

 Job: - Plotted
Job ID: 1 Sheet set name:
Date and time started: 24.10.2012 20:52:47 Date and time completed: 24.10.2012 20:52:49 UserID: Олег Profile ID: <<Unnamed Profile>> Total sheets: 1 Sheets plotted: 1 Number of errors: 0 Number of warnings: 0
Sheet: - Plotted
File: C:\Users\Олег\Programming\ACAD\#SNIPPETS\ACAD_INTEROP\Teste.dwg Category name: Page setup: Device name: DWG To PDF.pc3 - plotted to file Plot file path: C:\Users\Олег\Programming\ACAD\#SNIPPETS\ACAD_INTEROP\Teste_AR2.pdf Paper size: ISO A1 (841.00 x 594.00 MM)
 
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Member
lfecchio
Posts: 7
Registered: ‎10-19-2012
Message 5 of 6 (1,150 Views)

Re: Troubles in a C# interop to access a ActiveLayout.PlotType

10-29-2012 05:01 AM in reply to: lfecchio

Thanks for help!

 

I reinstall the ARX SDK for 2010 and works fine!

 

But, the DefaultPlotStyleTable insists in not work.

The code set all preferences, but the result (in pdf) doesn't reflect them.

 

For example, if I use the monochrome.ctb to plot into a pdf, it's doesn't plot in monochrome.

I saw too that a file stb raises a exception.

Active Member
lfecchio
Posts: 7
Registered: ‎10-19-2012
Message 6 of 6 (1,133 Views)

Re: Troubles in a C# interop to access a ActiveLayout.PlotType

10-30-2012 05:19 AM in reply to: lfecchio

Hi!

 

I include the line dm.ActiveLayout.StyleSheet = "AZEVEDO.ctb" and all works fine!

 

Thanks for help!!!!

 

Following code examples for those who have questions:

 

---------------------------------------------------------------------------------------------------------------

VB

---------------------------------------------------------------------------------------------------------------

      Dim oAcadApp As Autodesk.AutoCAD.Interop.AcadApplication
      Dim sAcadID = "AutoCAD.Application.18"

      ' Abre AutoCAD
      Try
         oAcadApp = DirectCast(System.Runtime.InteropServices.Marshal.GetActiveObject(sAcadID), Autodesk.AutoCAD.Interop.AcadApplication)
      Catch
         Dim AcadProg As System.Type = System.Type.GetTypeFromProgID(sAcadID)
         oAcadApp = DirectCast(System.Activator.CreateInstance(AcadProg), Autodesk.AutoCAD.Interop.AcadApplication)
      End Try
      oAcadApp.Visible = True
      Try
         ' Inicia ciclo de execução
         If oAcadApp IsNot Nothing Then
            Dim StyleTable As String = "AZEVEDO.ctb"
            ' set application to be visible
            oAcadApp.Visible = True
            ' expand window to max
            oAcadApp.WindowState = AcWindowState.acMax
            'could leave this false to hide Acad from the user
            Dim dm As Autodesk.AutoCAD.Interop.AcadDocument

            dm = oAcadApp.Documents.Open("d:\teste\teste.dwg", True, Nothing)
            ' set desired layouts first
            dm.ActiveLayout = dm.Layouts.Item(0)
            dm.SetVariable("BACKGROUNDPLOT", 0)

            dm.ActiveLayout.PlotType = AcPlotType.acExtents
            dm.ActiveLayout.StandardScale = AcPlotScale.ac1_1

            dm.ActiveLayout.PlotWithPlotStyles = True
            dm.ActiveLayout.UseStandardScale = True
            dm.ActiveLayout.CenterPlot = True
            dm.ActiveLayout.StyleSheet = "AZEVEDO.ctb"

            ' dm.ActiveLayout.CanonicalMediaName = "ISO_B5_(182.00_x_237.00_MM)"

            oAcadApp.Preferences.Output.AutomaticPlotLog = False
            oAcadApp.Preferences.Output.ContinuousPlotLog = False
            'oAcadApp.Preferences.Output.DefaultOutputDevice = "DWG To PDF.pc3"
            oAcadApp.Preferences.Output.DefaultPlotStyleTable = "AZEVEDO.ctb"
            dm.Regen(AcRegenType.acActiveViewport)

            dm.Plot.QuietErrorMode = True
            dm.Plot.NumberOfCopies = 1

            dm.ActiveLayout.RefreshPlotDeviceInfo()
            dm.Plot.PlotToFile("d:\teste\layouts.pdf", "C:\Program Files\AutoCAD 2009\UserDataCache\Plotters\DWG To PDF.pc3")

 

            'dm.Close(null, null);
            'oAcadApp.Quit();
         End If
      Catch ex As System.Exception
         MsgBox(ex.Message & vbLf & ex.StackTrace)

      End Try

 

 

---------------------------------------------------------------------------------------------------------------

C#

---------------------------------------------------------------------------------------------------------------

using System;
using System.IO;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;

namespace ProcessoCAD
{
   static class Program
   {
      private static Autodesk.AutoCAD.Interop.AcadApplication oAcadApp = null;
      private static string sAcadID = "AutoCAD.Application.18";

      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main()
      {
         // Abre AutoCAD
         try
         {
            oAcadApp = (Autodesk.AutoCAD.Interop.AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject(sAcadID);
         }
         catch
         {
            System.Type AcadProg = System.Type.GetTypeFromProgID(sAcadID);
            oAcadApp = (Autodesk.AutoCAD.Interop.AcadApplication)System.Activator.CreateInstance(AcadProg);
         }

         // Inicia ciclo de execução
         if (oAcadApp != null)
         {
            string StyleTable = "AZEVEDO.ctb";

            // App Visível
            oAcadApp.Visible = true;

            // Expande Janela
            oAcadApp.WindowState = AcWindowState.acMax;

            // Documento
            Autodesk.AutoCAD.Interop.AcadDocument dm;
            dm = oAcadApp.Documents.Open("d:\\teste\\Teste.dwg", true, null);

            // Plotagem Síncrona
            dm.SetVariable("BACKGROUNDPLOT", 0);

            // Seleciona Layout
            dm.ActiveSpace = AcActiveSpace.acPaperSpace;
            
            // Configurações da Plotagem
            dm.ActiveLayout.PlotType = AcPlotType.acExtents;
            dm.ActiveLayout.StandardScale = AcPlotScale.ac1_1;
            dm.ActiveLayout.PlotWithPlotStyles = true;
            dm.ActiveLayout.UseStandardScale = true;
            dm.ActiveLayout.CenterPlot = true;
            dm.ActiveLayout.StyleSheet = "AZEVEDO.ctb";

            // Aplica Estilo
            oAcadApp.Preferences.Output.DefaultPlotStyleTable = StyleTable;
            
            dm.Regen(AcRegenType.acActiveViewport);

            // Gera Arquivo
            dm.Plot.QuietErrorMode = true;
            dm.Plot.NumberOfCopies = 1;
            dm.ActiveLayout.RefreshPlotDeviceInfo();
            dm.Plot.PlotToFile("d:\\teste\\Teste.pdf", "C:\\Program Files\\AutoCAD 2010\\UserDataCache\\Plotters\\DWG To PDF.pc3");
            dm.Close(null, null);

            //oAcadApp.Quit();
         }
      }
    }
}

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.