Пример #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);
            }
        }
Пример #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);
                }
            }
        }
Пример #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);
            }
        }
Пример #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);
                }
            }
        }
Пример #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;
                }
            }
        }
Пример #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());
            }
        }
Пример #7
0
        public void AcadApp_EndOpen(string filename)
        {
            IsOpenError = false;
            DelLayouts();
            AcadApp.ActiveDocument.ActiveLayout = AcadApp.ActiveDocument.Layouts.Item("Model");
            int tN = AcadApp.ActiveDocument.ModelSpace.Count;

            for (int i = 0; i < tN; i++)
            {
                if (AcadApp.ActiveDocument.ModelSpace.Item(i).EntityName == "AcDbBlockReference")
                {
                    AcadBlockReference iBlockReference = (AcadBlockReference)AcadApp.ActiveDocument.ModelSpace.Item(i);
                    if (iBlockReference.EffectiveName == GlobalInfo.FrameEffectiveName)
                    {
                        string    iFVKey;
                        FrameView iFrameView;
                        iFVKey                = String.Format("{0, 3}", iBlockReference.GetAttributes()[GlobalInfo.FramePageAttrID].TextString);
                        iFrameView.Id         = i;
                        iFrameView.Page       = iBlockReference.GetAttributes()[GlobalInfo.FramePageAttrID].TextString;
                        iFrameView.LowerLeft  = new double[] { iBlockReference.InsertionPoint[0] - iBlockReference.XScaleFactor * GlobalInfo.PlotPageWidth, iBlockReference.InsertionPoint[1], 0 };
                        iFrameView.UpperRight = new double[] { iBlockReference.InsertionPoint[0], iBlockReference.InsertionPoint[1] + iBlockReference.YScaleFactor * GlobalInfo.PlotPageHeight, 0 };
                        if (!FrameViewList.ContainsKey(iFVKey))
                        {
                            FrameViewList.Add(iFVKey, iFrameView);
                        }
                    }
                }
            }
            FrameViewList = FrameViewList.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
            AcadApp.ActiveDocument.Layouts.Item("Model").ConfigName = GlobalInfo.ModelConfigName;
            AcadApp.ActiveDocument.Layouts.Item("Model").StyleSheet = StyleSheetName;
            foreach (KeyValuePair <string, FormatTask.FrameView> iFrameView in FrameViewList)
            {
                AddBlankLayout(iFrameView.Value.Page);
                AcadLayout iLayout = AcadApp.ActiveDocument.Layouts.Item(iFrameView.Value.Page);
                AcadApp.ActiveDocument.ActiveLayout = iLayout;
                iLayout.ConfigName         = GlobalInfo.PlotConfigName;
                iLayout.CanonicalMediaName = GlobalInfo.PlotCanonicalMediaName;
                iLayout.PlotRotation       = AcPlotRotation.ac0degrees;
                iLayout.PaperUnits         = AcPlotPaperUnits.acMillimeters;
                iLayout.SetWindowToPlot(new double[] { 0, 0 }, new double[] { GlobalInfo.PlotPageWidth, GlobalInfo.PlotPageHeight });
                iLayout.PlotType         = AcPlotType.acWindow;
                iLayout.UseStandardScale = true;
                iLayout.StandardScale    = AcPlotScale.ac1_1;
                iLayout.CenterPlot       = true;
                iLayout.StyleSheet       = StyleSheetName;
                AcadPViewport iPViewport = AcadApp.ActiveDocument.PaperSpace.AddPViewport(new double[] { GlobalInfo.PlotPageWidth / 2, GlobalInfo.PlotPageHeight / 2, 0 }, GlobalInfo.PlotPageWidth, GlobalInfo.PlotPageHeight);
                iPViewport.Display(true);
                iPViewport.Layer                       = "0";
                iPViewport.ViewportOn                  = true;
                AcadApp.ActiveDocument.MSpace          = true;
                AcadApp.ActiveDocument.ActivePViewport = iPViewport;
                AcadApp.ZoomWindow(iFrameView.Value.LowerLeft, iFrameView.Value.UpperRight);
                AcadApp.ActiveDocument.MSpace = false;
                iPViewport.DisplayLocked      = true;
            }
            AcadApp.ActiveDocument.ActiveLayout = AcadApp.ActiveDocument.Layouts.Item("Model");
            foreach (KeyValuePair <string, FormatTask.FrameView> iFrameView in FrameViewList)
            {
                AcadLayout iLayout = AcadApp.ActiveDocument.Layouts.Item(iFrameView.Value.Page);
                AcadApp.ActiveDocument.ActiveLayout = iLayout;
                AcadApp.ZoomAll();
            }
            DelLayout(GlobalInfo.TempLayoutName);
            AcadApp.ActiveDocument.ActiveLayout = AcadApp.ActiveDocument.Layouts.Item("Model");
            AcadApp.ZoomAll();
            IsSaveError = !SaveDocument();
            AcadApp.ActiveDocument.SendCommand("CLOSE\n");
        }