public static void ReframeLayouts(Document doc, string layerName)
        {
            Database db = doc.Database;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                // Collect all the paperspace layouts for plotting
                ObjectIdCollection layoutsToPlot = new ObjectIdCollection();

                foreach (ObjectId btrId in bt)
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);

                    if (btr.IsLayout && btr.Name.ToUpper() != BlockTableRecord.ModelSpace.ToUpper())
                    {
                        layoutsToPlot.Add(btrId);
                    }
                }

                foreach (ObjectId btrId in layoutsToPlot)
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForWrite);
                    Layout           lo  = (Layout)tr.GetObject(btr.LayoutId, OpenMode.ForRead);

                    ACADFunction.ChangeName(doc, layerName);

                    foreach (ObjectId vpId in lo.GetViewports())
                    {
                        Viewport vp = (Viewport)tr.GetObject(vpId, OpenMode.ForWrite);
                        vp.StandardScale = StandardScaleType.ScaleToFit;
                    }
                }

                tr.Commit();
            }
        }
        public static void OpenDocument(Document sourceDoc, DocumentCollection acDocMgr, string folderName)
        {
            Database sourceDB = sourceDoc.Database;

            //Create Export Directory
            string exportName = Path.GetFileNameWithoutExtension(sourceDoc.Name);
            string exportPath = folderName + @"\" + exportName;

            System.IO.DirectoryInfo di     = Directory.CreateDirectory(exportPath);
            System.IO.DirectoryInfo diDWG  = Directory.CreateDirectory(di.FullName + @"\DWG");
            System.IO.DirectoryInfo diDXF  = Directory.CreateDirectory(di.FullName + @"\DXF");
            System.IO.DirectoryInfo diADXF = Directory.CreateDirectory(di.FullName + @"\DXF FACE A");
            System.IO.DirectoryInfo diBDXF = Directory.CreateDirectory(di.FullName + @"\DXF FACE B");
            System.IO.DirectoryInfo diPDF  = Directory.CreateDirectory(di.FullName + @"\PDF");

            int i = 0;

            using (Transaction acTrans = sourceDB.TransactionManager.StartTransaction())
            {
                // This example returns the layer table for the current database
                LayerTable acLyrTbl;
                acLyrTbl = acTrans.GetObject(sourceDB.LayerTableId, OpenMode.ForRead) as LayerTable;

                string dwgFlatTemplate   = _templateFolder + "template_Plan.dwt";
                string dwgBombedTemplate = _templateFolder + "template_Bombed.dwt";
                string dxfTemplate       = _templateFolder + "acad.dwt";

                Document newDoc;
                Document bombedDoc = acDocMgr.Add(dwgBombedTemplate);
                Document planDoc   = acDocMgr.Add(dwgFlatTemplate);
                Document dxfDoc    = acDocMgr.Add(dxfTemplate);

                // Step through the Layer table
                foreach (ObjectId acObjId in acLyrTbl)
                {
                    LayerTableRecord acLyrTblRec;
                    acLyrTblRec = acTrans.GetObject(acObjId, OpenMode.ForRead) as LayerTableRecord;

                    //Active the source document
                    if (acDocMgr.MdiActiveDocument != sourceDoc)
                    {
                        acDocMgr.MdiActiveDocument = sourceDoc;
                    }

                    //Retrive Elements by Layers
                    string             panelName          = acLyrTblRec.Name;
                    ObjectIdCollection collection         = Selection.SelectEntities(sourceDoc, panelName);
                    ObjectIdCollection checkingCollection = Selection.CheckingCollection(sourceDoc, panelName);

                    if (collection.Count != 0)
                    {
                        if (checkingCollection.Count == 0)
                        {
                            newDoc = planDoc;
                        }
                        else
                        {
                            newDoc = bombedDoc;
                        }

                        //Lock the new doc
                        using (DocumentLock Aclock = newDoc.LockDocument())
                        {
                            //Paste the object in it
                            ObjectIdCollection pastedElementsIds = ACADFunction.CopyElements(newDoc, collection, sourceDB, false);

                            //Active the document
                            if (acDocMgr.MdiActiveDocument != newDoc)
                            {
                                acDocMgr.MdiActiveDocument = newDoc;
                            }

                            ACADFunction.ApplyResultLayer(newDoc, panelName);

                            ACADFunction.ReframeLayouts(newDoc, panelName);


                            ////Print it - Deprecated
                            //while (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
                            //{
                            //    System.Threading.Thread.Sleep(500);
                            //}

                            //if (i < 5)
                            //{

                            //}
                            //Tools.MultiSheetPlot(newDoc, diPDF, panelName);

                            //Purge unused layer
                            ACADFunction.PurgeLayers(newDoc);

                            //Save It
                            Tools.SaveDoc(newDoc, diDWG, panelName);

                            //Remove object for the next run
                            ACADFunction.EraseElements(newDoc, pastedElementsIds);
                        }

                        //Active the source document
                        if (acDocMgr.MdiActiveDocument != sourceDoc)
                        {
                            acDocMgr.MdiActiveDocument = sourceDoc;
                        }

                        //Select the face A polyline
                        ObjectIdCollection cyanPolyline = Selection.SelectCyanLines(sourceDoc, panelName);

                        //Seclect the face B polyline
                        ObjectIdCollection greenPolyline = Selection.SelectGreenLines(sourceDoc, panelName);

                        //Active the dxf document
                        if (acDocMgr.MdiActiveDocument != dxfDoc)
                        {
                            acDocMgr.MdiActiveDocument = dxfDoc;
                        }

                        //Lock the new doc
                        using (DocumentLock Aclock = dxfDoc.LockDocument())
                        {
                            //Paste the object in it
                            ObjectIdCollection pastedpolylinesIds = ACADFunction.CopyElements(dxfDoc, cyanPolyline, sourceDB, true);

                            //Purge unused layer
                            ACADFunction.PurgeLayers(dxfDoc);

                            //Save it
                            if (checkingCollection.Count != 0)
                            {
                                Tools.SaveDoc(dxfDoc, diADXF, panelName + "A");
                            }
                            else
                            {
                                Tools.SaveDoc(dxfDoc, diDXF, panelName);
                            }

                            //remove the elements for the next run
                            ACADFunction.EraseElements(dxfDoc, pastedpolylinesIds);
                        }

                        //Check for panel curvature
                        if (checkingCollection.Count != 0)
                        {
                            //Lock the new doc
                            using (DocumentLock Aclock = dxfDoc.LockDocument())
                            {
                                //Paste the object in it
                                ObjectIdCollection pastedpolylinesIds = ACADFunction.CopyElements(dxfDoc, greenPolyline, sourceDB, true);

                                //Purge unused layer
                                ACADFunction.PurgeLayers(dxfDoc);

                                //Save it
                                Tools.SaveDoc(dxfDoc, diBDXF, panelName + "B");

                                //remove the elements for the next run
                                ACADFunction.EraseElements(dxfDoc, pastedpolylinesIds);
                            }
                        }
                    }

                    i++;

                    string logPath = _templateFolder + @"\" + exportName + @".txt";
                    File.WriteAllLines(logPath, LogFile.LogList.ToArray());
                }
            }
        }
Exemple #3
0
        static public void MultiSheetPlot(Document doc, DirectoryInfo diPDF, string panelName)
        {
            Editor   ed = doc.Editor;
            Database db = doc.Database;

            Transaction tr = db.TransactionManager.StartTransaction();

            using (tr)
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                PlotInfo          pi  = new PlotInfo();
                PlotInfoValidator piv = new PlotInfoValidator();
                piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;

                // A PlotEngine does the actual plotting (can also create one for Preview)

                if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
                {
                    PlotEngine pe = PlotFactory.CreatePublishEngine();
                    using (pe)
                    {
                        // Collect all the paperspace layouts for plotting
                        ObjectIdCollection      layoutsToPlot     = new ObjectIdCollection();
                        List <BlockTableRecord> btrListToBeSorted = new List <BlockTableRecord>();

                        foreach (ObjectId btrId in bt)
                        {
                            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);

                            if (btr.IsLayout && btr.Name.ToUpper() != BlockTableRecord.ModelSpace.ToUpper())
                            {
                                layoutsToPlot.Add(btrId);
                            }
                        }

                        //Sort the layout by name
                        btrListToBeSorted.Sort((x, y) => string.Compare(x.Name, y.Name));

                        foreach (BlockTableRecord btr in btrListToBeSorted)
                        {
                            layoutsToPlot.Add(btr.Id);
                        }

                        // Create a Progress Dialog to provide info and allow thej user to cancel
                        PlotProgressDialog ppd = new PlotProgressDialog(false, layoutsToPlot.Count, true);

                        using (ppd)
                        {
                            int numSheet = 1;

                            foreach (ObjectId btrId in layoutsToPlot)
                            {
                                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForWrite);
                                Layout           lo  = (Layout)tr.GetObject(btr.LayoutId, OpenMode.ForRead);

                                // We need a PlotSettings object based on the layout settings which we then customize

                                PlotSettings ps = new PlotSettings(lo.ModelType);
                                ps.CopyFrom(lo);

                                // The PlotSettingsValidator helps create a valid PlotSettings object
                                PlotSettingsValidator psv = PlotSettingsValidator.Current;

                                // We'll plot the extents, centered and scaled to fit
                                psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
                                psv.SetUseStandardScale(ps, true);
                                psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
                                psv.SetPlotCentered(ps, true);


                                // We'll use the standard PDF, as this supports multiple sheets
                                //Think about changing the DWG To PDF settings to custom value for setting up printing margin
                                //psv.SetPlotConfigurationName(ps,"DWFx ePlot (XPS Compatible).pc3", "ANSI_A_(8.50_x_11.00_Inches)");
                                //psv.SetPlotConfigurationName(ps, "DWG To PDF_Custom.pc3", "ISO_A4_(210.00_x_297.00_MM)");
                                psv.SetPlotConfigurationName(ps, "DWG To PDF_Custom.pc3", "ISO_expand_A4_(210.00_x_297.00_MM)");

                                // We need a PlotInfo object linked to the layout

                                pi.Layout = btr.LayoutId;

                                // Make the layout we're plotting current
                                LayoutManager.Current.CurrentLayout = lo.LayoutName;

                                //Change the name in it
                                ACADFunction.ChangeName(doc, panelName);

                                foreach (ObjectId vpId in lo.GetViewports())
                                {
                                    Viewport vp = (Viewport)tr.GetObject(vpId, OpenMode.ForWrite);
                                    vp.StandardScale = StandardScaleType.ScaleToFit;
                                    //vp.CustomScale = 10;
                                }

                                // We need to link the PlotInfo to the PlotSettings and then validate it

                                pi.OverrideSettings = ps;
                                piv.Validate(pi);

                                if (numSheet == 1)
                                {
                                    ppd.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Custom Plot Progress");
                                    ppd.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job");
                                    ppd.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
                                    ppd.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
                                    ppd.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");
                                    ppd.LowerPlotProgressRange = 0;
                                    ppd.UpperPlotProgressRange = 100;
                                    ppd.PlotProgressPos        = 0;

                                    // Let's start the plot, at last

                                    ppd.OnBeginPlot();
                                    ppd.IsVisible = true;
                                    pe.BeginPlot(ppd, null);

                                    // We'll be plotting a single document
                                    // Let's plot to file
                                    pe.BeginDocument(pi, doc.Name, null, 1, true, diPDF.FullName + @"\" + panelName + ".pdf");
                                }

                                // Which may contain multiple sheets

                                ppd.StatusMsgString = "Plotting " + doc.Name.Substring(doc.Name.LastIndexOf("\\") + 1) + " - sheet " + numSheet.ToString() + " of " + layoutsToPlot.Count.ToString();

                                ppd.OnBeginSheet();

                                ppd.LowerSheetProgressRange = 0;
                                ppd.UpperSheetProgressRange = 100;
                                ppd.SheetProgressPos        = 0;

                                PlotPageInfo ppi = new PlotPageInfo();
                                pe.BeginPage(ppi, pi, (numSheet == layoutsToPlot.Count), null);
                                pe.BeginGenerateGraphics(null);
                                ppd.SheetProgressPos = 50;
                                pe.EndGenerateGraphics(null);

                                // Finish the sheet
                                pe.EndPage(null);
                                ppd.SheetProgressPos = 100;
                                ppd.OnEndSheet();
                                numSheet++;
                            }

                            // Finish the document

                            pe.EndDocument(null);

                            // And finish the plot

                            ppd.PlotProgressPos = 100;
                            ppd.OnEndPlot();
                            pe.EndPlot(null);
                        }
                    }
                }
                else
                {
                    ed.WriteMessage(
                        "\nAnother plot is in progress."
                        );
                }

                tr.Commit();
            }
        }