Ejemplo n.º 1
0
        private static IEnumerable <string> GetPrinterList()
        {
            try
            {
                using (var cad = new AutoCadConnector())
                {
                    AcadDocument doc;
                    if (cad.Application.Documents.Count > 0)
                    {
                        doc = cad.Application.Documents.Item(0) ?? cad.Application.Documents.Add();
                    }
                    else
                    {
                        doc = cad.Application.Application.Documents.Add();
                    }

                    AcadLayout layout = doc.ModelSpace.Layout;

                    layout.RefreshPlotDeviceInfo();
                    var plots = (string[])layout.GetPlotDeviceNames();

                    if (cad.Application.Documents.Count > 0)
                    {
                        doc.Close(false, "");
                    }
                    return(plots);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("程序运行出错,错误描述:{0}", ex.Message), @"批量打印", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return(null);
            }
        }
Ejemplo n.º 2
0
        private void cbSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (var cad = new AutoCadConnector())
            {
                try
                {
                    AcadDocument doc;
                    if (cad.Application.Documents.Count > 0)
                    {
                        doc = cad.Application.Documents.Item(0) ?? cad.Application.Documents.Add();
                    }
                    else
                    {
                        doc = cad.Application.Application.Documents.Add();
                    }

                    AcadLayout layout = doc.ModelSpace.Layout;

                    layout.ConfigName         = cbPrinterList.Text;
                    layout.CanonicalMediaName = mediaNameDictionary[cbSize.Text];

                    double w, h;
                    layout.GetPaperSize(out w, out h);
                    layout.PlotRotation = w > h ? AcPlotRotation.ac0degrees : AcPlotRotation.ac90degrees;
                    layout.RefreshPlotDeviceInfo();
                    lblPaperSize.Text = string.Format("打印尺寸:{0}*{1}", Math.Ceiling(w), Math.Ceiling(h));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("调用CAD程序出错!\n错误描述:{0}", ex.Message), @"批量打印", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Plot with override configuration
        /// </summary>
        /// <param name="acApp">AutoCAD application</param>
        /// <param name="plotConfig">Plot override Configuration</param>
        public void Plot(AcadApplication acApp, EPlotConfig pConfig)
        {
            AcadDocuments acDocs = acApp.Documents;
            int           opFlag = DocIndex(acDocs);

            if (Open(acApp))
            {
                AcadLayout acLot = acApp.ActiveDocument.ActiveLayout;

                // Store plot configuration of active layout
                double      numerator, denominator;
                EPlotConfig strConfig = new EPlotConfig(
                    acLot.ConfigName, acLot.CanonicalMediaName, acLot.StyleSheet,
                    acLot.PlotType, acLot.StandardScale, acLot.UseStandardScale,
                    1, acLot.CenterPlot, acLot.PlotRotation, false,
                    acLot.ScaleLineweights);//, acLot.PlotOrigin);
                acLot.GetCustomScale(out numerator, out denominator);

                try
                {
                    // Setting layout for plot
                    acLot.ConfigName         = pConfig.Plotter;
                    acLot.CanonicalMediaName = pConfig.CanonicalMediaName;
                    acLot.StyleSheet         = pConfig.PlotStyleTable;
                    acLot.PlotType           = pConfig.PlotType;
                    acLot.StandardScale      = pConfig.StandardScale;
                    acLot.UseStandardScale   = pConfig.UseStandardScale;
                    // NOTE: -----------------------------------------------------
                    // ROTATION AUTOMATICALLY FEATURE WILL BE WRITTEN IN NEXT TIME
                    // -----------------------------------------------------------
                    acLot.PlotRotation     = pConfig.PlotRotation;
                    acLot.ScaleLineweights = pConfig.IsScaleLineweight;
                    //acLot.PlotOrigin = pConfig.PlotOrigin;
                    acLot.SetCustomScale(pConfig.CustomScale, 1);

                    // Update plot config then Plot
                    acLot.RefreshPlotDeviceInfo();
                    acApp.ActiveDocument.Plot.PlotToDevice();
                }
                catch { }

                // Restore layout setting of plot
                acLot.ConfigName         = strConfig.Plotter;
                acLot.CanonicalMediaName = strConfig.CanonicalMediaName;
                acLot.StyleSheet         = strConfig.PlotStyleTable;
                acLot.PlotType           = strConfig.PlotType;
                acLot.StandardScale      = strConfig.StandardScale;
                acLot.UseStandardScale   = strConfig.UseStandardScale;
                acLot.PlotRotation       = strConfig.PlotRotation;
                acLot.ScaleLineweights   = strConfig.IsScaleLineweight;
                //acLot.PlotOrigin = strConfig.PlotOrigin;
                acLot.SetCustomScale(numerator, denominator);
            }

            // If sheet is not opened initially, closing this after plot finish
            if (opFlag == -1 && DocIndex(acDocs) != -1)
            {
                acDocs.Item(DocIndex(acDocs)).Close(false);
            }
        }
Ejemplo n.º 4
0
        private void cbPrinterList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsInit)
            {
                return;
            }
            if (cbPrinterList.SelectedIndex < 0)
            {
                return;
            }

            using (var cad = new AutoCadConnector())
            {
                try
                {
                    AcadDocument doc;
                    if (cad.Application.Documents.Count > 0)
                    {
                        doc = cad.Application.Documents.Item(0) ?? cad.Application.Documents.Add();
                    }
                    else
                    {
                        doc = cad.Application.Application.Documents.Add();
                    }

                    AcadLayout layout = doc.ModelSpace.Layout;

                    if (cbPrinterList.Text != @"无" || string.IsNullOrEmpty(cbPrinterList.Text))
                    {
                        layout.RefreshPlotDeviceInfo();
                        layout.ConfigName = cbPrinterList.Text;
                        var p = (string[])layout.GetCanonicalMediaNames();
                        cbSize.Items.Clear();
                        mediaNameDictionary.Clear();
                        foreach (var s in p)
                        {
                            var LocaleMediaName = layout.GetLocaleMediaName(s);
                            cbSize.Items.Add(LocaleMediaName);
                            mediaNameDictionary[LocaleMediaName] = s;
                        }
                        //cbSize.DataSource = p;
                    }
                    else
                    {
                        cbSize.Items.Clear();
                        mediaNameDictionary.Clear();
                    }
                    doc.Close(false, "");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("调用CAD程序出错!\n错误描述:{0}", ex.Message), @"批量打印", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Ejemplo n.º 5
0
        public void Print()
        {
            using (var cad = new AutoCadConnector())
            {
                try
                {
                    AcadDocument doc    = cad.Application.Documents.Open(FileFullName);
                    AcadLayout   layout = doc.ModelSpace.Layout;

                    #region Autocad.Layout对象的属性及方法,主要用于打印输出

                    //Application
                    //Block
                    //CanonicalMediaName
                    //CenterPlot
                    //ConfigName
                    //Document
                    //GetCanonicalMediaNames
                    //GetLocaleMediaName
                    //GetPlotDeviceNames
                    //GetPlotStyleTableNames
                    //Handle
                    //HasExtensionDictionary
                    //ModelType
                    //Name
                    //ObjectID
                    //ObjectName
                    //OwnerID
                    //PaperUnits
                    //PlotHidden
                    //PlotOrigin
                    //PlotRotation
                    //PlotType
                    //PlotViewportBorders
                    //PlotViewportsFirst
                    //PlotWithLineweights
                    //PlotWithPlotStyles
                    //RefreshPlotDeviceInfo
                    //ScaleLineweights
                    //ShowPlotStyles
                    //StandardScale
                    //StyleSheet
                    //TabOrder
                    //UseStandardScale
                    //ViewToPlot

                    #endregion

                    //layout.RefreshPlotDeviceInfo();

                    //ConfigName    指定打印机配置名。用于布局或打印配置的 PC3 文件或打印设备的名称。
                    layout.ConfigName = PlotConfigName;

                    layout.StyleSheet = StyleSheet;

                    #region

                    /*
                     * //PlotType  指定布局或打印配置的类型。
                     * //acDisplay 打印当前显示的所有对象。
                     * //acExtents 打印当前选定空间范围内的所有对象。
                     * //acLimits 打印当前空间界限内的所有对象。
                     * //acView 打印在ViewToPlot属性中命名的视口。
                     * //acWindow 打印在SetWindowToPlot方法中指定的窗口中的所有对象。
                     * //acLayout 打印指定纸张尺寸边界内的所有对象,它的原点是由布局选项卡中从坐标(0,0)计算得到。该选项在从模型空间打印时不可用。
                     *
                     * */

                    #endregion

                    layout.PlotType = AcPlotType.acExtents;

                    //PlotWithPlotStyles:指定对象是按在打印样式文件中分配的配置打印,还是按图形文件中的配置打印。
                    layout.PlotWithPlotStyles = true;

                    //CanonicalMediaName    按名称指定图纸尺寸.String[字符串]; 可读写,图纸尺寸的名称
                    layout.CanonicalMediaName = CanonicalMediaName;
                    layout.PaperUnits         = AcPlotPaperUnits.acMillimeters;
                    layout.PlotOrigin         = new double[] { 0, 0 };
                    layout.UseStandardScale   = true;
                    layout.StandardScale      = AcPlotScale.acScaleToFit;
                    if (MapSheet == "A3" || MapSheet == "A4")
                    {
                        layout.PlotRotation = Angle == 0 ? AcPlotRotation.ac90degrees : AcPlotRotation.ac0degrees;
                    }
                    else
                    {
                        layout.PlotRotation = Angle == 90 ? AcPlotRotation.ac90degrees : AcPlotRotation.ac0degrees;
                    }
                    layout.RefreshPlotDeviceInfo();
                    double w, h;
                    layout.GetPaperSize(out w, out h);
                    Console.WriteLine(@"图幅:{0},{1}*{2}", CanonicalMediaName, w, h);

                    doc.Plot.PlotToDevice(null);
                    doc.Close(false);

                    PrintedNum++;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("调用CAD执行批量打印时发生错误!\n错误描述:{0}", ex.Message), @"批量打印",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    throw;
                }
            }
        }
Ejemplo n.º 6
0
        public static void ToPDF()
        {
            Document activeDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;

            AcadDocument ThisDrawing = activeDoc.GetAcadDocument() as AcadDocument;

            AcadLayout layout    = ThisDrawing.ActiveLayout;
            String     MediaName = layout.CanonicalMediaName;

            if (MediaName.Equals(""))
            {
                activeDoc.Editor.WriteMessage("There is no media set for the active layout.");
                return;
            }
            else
            {
                activeDoc.Editor.WriteMessage("The media for the active layout is: " + MediaName);
            }

            try
            {
                AcadPlotConfiguration oplot = ThisDrawing.PlotConfigurations.Add("PDF", layout.ModelType);

                oplot.PaperUnits = AcPlotPaperUnits.acMillimeters;

                oplot.StyleSheet = "monochrome.ctb";

                oplot.PlotWithPlotStyles = true;

                oplot.ConfigName = "DWG To PDF.pc3";

                oplot.UseStandardScale = true;

                oplot.StandardScale = AcPlotScale.acScaleToFit;

                oplot.PlotType = AcPlotType.acExtents;

                oplot.CenterPlot = true;

                Object    oMediaNames = layout.GetCanonicalMediaNames();
                ArrayList mediaNames  = new ArrayList((string[])oMediaNames);
                foreach (String sName in mediaNames)
                {
                    if (sName.Contains(MediaName))

                    {
                        oplot.CanonicalMediaName = sName;

                        layout.CopyFrom(oplot);

                        layout.PlotRotation = AcPlotRotation.ac0degrees;

                        layout.RefreshPlotDeviceInfo();



                        ThisDrawing.SetVariable("BACKGROUNDPLOT", 0);

                        ThisDrawing.Plot.QuietErrorMode = true;



                        ThisDrawing.Plot.PlotToFile("c://temp//d1.pdf", "DWG To PDF.pc3");

                        oplot.Delete();

                        oplot = null;

                        return;
                    }
                }
            }

            catch (System.Exception es)
            {
                // System.Windows.Forms.MessageBox.Show(es.ToString());
            }
        }