Exemple #1
0
        void unitsConverted(PlotPaperUnit prevUnits, PlotPaperUnit plotPaperUnits)
        {
            double dCoeff = 0;

            if (plotPaperUnits == PlotPaperUnit.Millimeters && prevUnits == PlotPaperUnit.Inches)
            {
                dCoeff = 25.4;
            }
            else if (plotPaperUnits == PlotPaperUnit.Inches && prevUnits == PlotPaperUnit.Millimeters)
            {
                dCoeff = 1.0 / 25.4;
            }
            else
            {
                return;
            }

            bool bStandardScale = m_plotStg.UseStandardScale;

            if (bStandardScale)
            {
                double dStandardScale = m_plotStg.StdScale;
                if (dStandardScale != 0) // skip Fit to paper
                {
                    m_plotSettingVal.SetCustomPrintScale(m_plotStg, new CustomScale(dStandardScale, 1.0 / dCoeff));
                }
            }
            else
            {
                CustomScale cs = m_plotStg.CustomPrintScale;
                m_plotSettingVal.SetCustomPrintScale(m_plotStg, new CustomScale(cs.Numerator, cs.Denominator / dCoeff));
            }
        }
Exemple #2
0
        private void textBoxPlotOffsetX_TextChanged(object sender, EventArgs e)
        {
            if (textBoxPlotOffsetX.Text.Length > 0 && textBoxPlotOffsetY.Text.Length > 0)
            {
                PlotPaperUnit ppu = m_plotStg.PlotPaperUnits;
                if (ppu == PlotPaperUnit.Inches)
                {
                    textBoxPlotOffsetX.Text = (double.Parse(textBoxPlotOffsetX.Text) * unitsScale[(int)ppu].m_dScale).ToString();
                    textBoxPlotOffsetY.Text = (double.Parse(textBoxPlotOffsetY.Text) * unitsScale[(int)ppu].m_dScale).ToString();
                }

                if (isWHSwap())
                {
                    m_plotSettingVal.SetPlotOrigin(m_plotStg, new Point2d(double.Parse(textBoxPlotOffsetY.Text), double.Parse(textBoxPlotOffsetX.Text)));
                }
                else
                {
                    m_plotSettingVal.SetPlotOrigin(m_plotStg, new Point2d(double.Parse(textBoxPlotOffsetX.Text), double.Parse(textBoxPlotOffsetY.Text)));
                }

                FillPaperOrientation();
                FillPlotOffset(); // possibly offset was changed in validator
                FillScaleValues(false);
            }
        }
Exemple #3
0
        private void comboBoxPaperSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            String newLocaleMediaName    = comboBoxPaperSize.SelectedItem.ToString();
            String newCanonicalMediaName = getCanonicalByLocaleMediaName(newLocaleMediaName);

            m_plotSettingVal.SetCanonicalMediaName(m_plotStg, newCanonicalMediaName);

            PlotPaperUnit nativeUnits = getMediaNativePPU();

            // change paper orientation to dialog values
            PlotRotation pr;

            if (isPaperWidthLessHeight())
            {
                pr = (PlotRotation)(Convert.ToInt32(radioButtonLandscape.Checked) + Convert.ToInt32(checkBoxPlotUpsideDown.Checked) * 2);
            }
            else
            {
                pr = (PlotRotation)(Convert.ToInt32(!radioButtonLandscape.Checked) + Convert.ToInt32(checkBoxPlotUpsideDown.Checked) * 2);
            }
            m_plotSettingVal.SetPlotRotation(m_plotStg, pr);

            FillPaperOrientation();
            FillScaleValues(false);
            FillPlotOffset();

            // and reset units to paper native
            if (nativeUnits == PlotPaperUnit.Inches || nativeUnits == PlotPaperUnit.Millimeters)
            {
                comboBoxMMInches.SelectedIndex = (nativeUnits == PlotPaperUnit.Millimeters)?1:0;
            }
        }
Exemple #4
0
        void FillMMInches()
        {
            PlotPaperUnit ppu  = m_plotStg.PlotPaperUnits;
            bool          bTmp = (ppu == PlotPaperUnit.Pixels);

            if (comboBoxMMInches.Items.Count != (bTmp ? 1 : 2))
            {
                comboBoxMMInches.Items.Clear();
                if (bTmp)
                {
                    comboBoxMMInches.Items.Add("pixels");
                }
                else
                {
                    comboBoxMMInches.Items.Add("inches");
                    comboBoxMMInches.Items.Add("mm");
                }
            }
            int iIndex = (ppu == PlotPaperUnit.Millimeters) ? 1 : 0;

            if (comboBoxMMInches.Enabled == bTmp)
            {
                comboBoxMMInches.Enabled = !bTmp;
            }
            if (comboBoxMMInches.SelectedIndex != iIndex)
            {
                comboBoxMMInches.SelectedIndex = iIndex;
            }
        }
        public static void MainTesting()
        {
            Document      doc    = Application.DocumentManager.MdiActiveDocument;
            Database      db     = doc.Database;
            LayoutManager laymgr = LayoutManager.Current;

            try
            {
                Layout lay = doc.Create_NewLayout("Testing");

                double        w       = 25.00;
                double        h       = 30.00;
                PlotPaperUnit ppunits = PlotPaperUnit.Inches;

                lay.SetPageSize(w, h, ppunits);
            }

            catch
            {
            }
        }
Exemple #6
0
        void FillPlotOffset()
        {
            checkBoxCenterPlot.Enabled = m_plotStg.PlotCentered;
            textBoxPlotOffsetX.Enabled = !checkBoxCenterPlot.Enabled;
            textBoxPlotOffsetY.Enabled = !checkBoxCenterPlot.Enabled;

            Point2d plOrg = m_plotStg.PlotOrigin;

            if (isWHSwap())
            {
                textBoxPlotOffsetX.Text = plOrg.Y.ToString();
                textBoxPlotOffsetY.Text = plOrg.X.ToString();
            }
            else
            {
                textBoxPlotOffsetX.Text = plOrg.X.ToString();
                textBoxPlotOffsetY.Text = plOrg.Y.ToString();
            }

            PlotPaperUnit ppu = m_plotStg.PlotPaperUnits;

            if (ppu == PlotPaperUnit.Inches)
            {
                textBoxPlotOffsetX.Text = (double.Parse(textBoxPlotOffsetX.Text) / unitsScale[(int)ppu].m_dScale).ToString();
                textBoxPlotOffsetY.Text = (double.Parse(textBoxPlotOffsetY.Text) / unitsScale[(int)ppu].m_dScale).ToString();
            }

            Extents2d extMarg = m_plotStg.PlotPaperMargins;

            textBoxLeft.Text   = extMarg.MinPoint.X.ToString();
            textBoxRight.Text  = extMarg.MaxPoint.X.ToString();
            textBoxTop.Text    = extMarg.MinPoint.Y.ToString();
            textBoxBottom.Text = extMarg.MaxPoint.Y.ToString();

            textBoxCanonicalPaperName.Text = m_plotStg.CanonicalMediaName;
            label9.Text = unitsInfo.getTextByValue(double.Parse(textBoxPlotOffsetX.Text), unitsScale[(int)ppu]);
            label8.Text = unitsInfo.getTextByValue(double.Parse(textBoxPlotOffsetY.Text), unitsScale[(int)ppu]);
        }
Exemple #7
0
        private void comboBoxMMInches_SelectedIndexChanged(object sender, EventArgs e)
        {
            String unitsStr = comboBoxMMInches.SelectedItem.ToString();

            PlotPaperUnit plotPaperUnits = PlotPaperUnit.Pixels;

            if (unitsStr == "mm")
            {
                plotPaperUnits = PlotPaperUnit.Millimeters;
            }
            else if (unitsStr == "inches")
            {
                plotPaperUnits = PlotPaperUnit.Inches;
            }

            PlotPaperUnit prevUnits = m_plotStg.PlotPaperUnits;

            m_plotSettingVal.SetPlotPaperUnits(m_plotStg, plotPaperUnits);
            unitsConverted(prevUnits, plotPaperUnits);

            FillScaleValues(false);
            FillPlotOffset();
        }
        public static void SetPageSize(this Layout curlay, double width, double height, PlotPaperUnit ppu)
        {
            Document      doc    = Application.DocumentManager.MdiActiveDocument;
            Database      db     = doc.Database;
            LayoutManager laymgr = LayoutManager.Current;

            using (DocumentLock dl = doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    using (PlotSettings pltset = new PlotSettings(curlay.ModelType))
                    {
                        pltset.CopyFrom(curlay);
                        PlotSettingsValidator psv = PlotSettingsValidator.Current;
                        Layout layout             = new Layout();

                        try
                        {
                            if (curlay.ObjectId != null)
                            {
                                layout = tr.GetObject(curlay.ObjectId, OpenMode.ForWrite) as Layout;
                            }
                        }

                        catch
                        {
                        }

                        #region Paper Types
                        try
                        {
                            if (!ppu.Equals(PlotPaperUnit.Inches))
                            {
                                psv.SetPlotPaperUnits(pltset, PlotPaperUnit.Inches);
                            }

                            if ((width == 11.00 && height == 17.00) || (width == 17.00 && height == 11.00))
                            {
                                psv.SetPlotConfigurationName(pltset,
                                                             "DWG To PDF.pc3",
                                                             "ANSI_full_bleed_B_(11.00_x_17.00_Inches)");
                            }

                            if ((width == 18.00 && height == 24.00) || (width == 24.00 && height == 18.00))
                            {
                                psv.SetPlotConfigurationName(pltset,
                                                             "DWG To PDF.pc3",
                                                             "ARCH_full_bleed_B_(18.00_x_24.00_Inches)");
                            }

                            else if ((width == 17.00 && height == 22.00) || (width == 22.00 && height == 17.00))
                            {
                                psv.SetPlotConfigurationName(pltset,
                                                             "DWG To PDF.pc3",
                                                             "ARCH_full_bleed_C_(17.00_x_22.00_Inches)");
                            }

                            else if ((width == 24.00 && height == 36.00) || (width == 36.00 && height == 24.00))
                            {
                                psv.SetPlotConfigurationName(pltset,
                                                             "DWG To PDF.pc3",
                                                             "ARCH_full_bleed_D_(24.00_x_36.00_Inches)");
                            }

                            else if ((width == 36.00 && height == 48.00) || (width == 48.00 && height == 36.00))
                            {
                                psv.SetPlotConfigurationName(pltset,
                                                             "DWG To PDF.pc3",
                                                             "ARCH_full_bleed_E_(36.00_x_48.00_Inches)");
                            }

                            else if ((width == 30.00 && height == 42.00) || (width == 42.00 && height == 30.00))
                            {
                                psv.SetPlotConfigurationName(pltset,
                                                             "DWG To PDF.pc3",
                                                             "ARCH_full_bleed_E1_(30.00_x_42.00_Inches)");
                            }

                            else
                            {
                                doc.Editor.WriteMessage("\n The drawing sizes were invalid, default setting will be used");
                                psv.SetPlotConfigurationName(pltset,
                                                             "DWG To PDF.pc3",
                                                             "ARCH_full_bleed_E_(36.00_x_48.00_Inches)");
                            }
                        }

                        catch
                        {
                            doc.Editor.WriteMessage("\n The drawing sizes were invalid, default setting will be used");
                            psv.SetPlotConfigurationName(pltset,
                                                         "DWG To PDF.pc3",
                                                         "ARCH_full_bleed_E_(36.00_x_48.00_Inches)");
                        }
                        #endregion

                        psv.SetZoomToPaperOnUpdate(pltset, true);

                        layout.UpgradeOpen();
                        layout.CopyFrom(pltset);

                        doc.Editor.WriteMessage("\n  " + "\nNew device name: " + layout.PlotConfigurationName);

                        tr.Commit();
                    }
                }
            }
        }
Exemple #9
0
        public static void Main()
        {
            DocumentCollection mgr_doc = global::Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;

            //Access and lock the current document and database
            Document doc = global::Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db  = doc.Database;
            Editor   ed  = doc.Editor;

            HostApplicationServices.WorkingDatabase = db;

            AutoCADCommand.Audit();
            AutoCADCommand.Purge();

            // if the drawing has any AEC or Proxy objects, immediately
            // send out to AECTOACAD, audit, purge and then start work

            //doc.SendStringToExecute(
            //    "AECTOACAD",
            //    true,
            //    true,
            //    true);

            //Setup directories
            string curloc = Path.GetDirectoryName(db.Filename);

            //Creates a new directory for all files to be saved to
            //called "_Setup Files" and "Xrefs"
            string NewFol    = "_Setup Files";
            string NewSetups = "SETUP";
            string setuploc  = System.IO.Path.Combine(curloc, NewFol, NewSetups);
            string xrefloc   = System.IO.Path.Combine(curloc, NewFol);

            //creates the setup directories if they don't already exist
            if (!Directory.Exists(xrefloc))
            {
                Directory.CreateDirectory(xrefloc);
            }
            if (!Directory.Exists(setuploc))
            {
                Directory.CreateDirectory(setuploc);
            }

            //Gather all the xrefs from the document, if there are any
            List <BlockTableRecord> xlist = Xref_List(doc);

            //Get a list of layouts/layoutids
            List <Layout>   listoflayouts = Project_ListOfLayouts(doc);
            List <ObjectId> listoflayids  = Project_ListOfLayoutIds(doc);

            // The objectIDs for the BlockTableRecords of the Model and PaperSpace layouts
            ObjectId        mspace  = listoflayids[0];
            List <ObjectId> pspaces = listoflayids.GetRange(1, (listoflayids.Count() - 1));
            DictOfViewPorts vp_dict = new DictOfViewPorts();

            #region External Reference Exportation
            using (DocumentLock doclock = doc.LockDocument())
            {
                //Start the process of saving out each of the unique
                //xrefs into another folder called "Xrefs"
                if (xlist.Count() > 0)
                {
                    foreach (BlockTableRecord x in xlist)
                    {
                        using (Transaction tr = db.TransactionManager.StartTransaction())
                        {
                            //if the file already exists somewhere else in the directory
                            //just copy it from there to the base location instead
                            Xref_ExporterCopy(x);
                        }
                    }
                }
                #endregion

                #region Viewport Information
                //get the info on all viewports in all layouts
                foreach (Layout layout in listoflayouts)
                {
                    List <AutoCADViewport> list_vpinfo = Xref_ViewPortInfo(layout.ObjectId);
                    vp_dict.Add(layout, list_vpinfo);
                }
            }
            #endregion

            #region Actual Setup File Creation
            #region Misc. Saving
            //doc is no longer locked
            //save the consultant drawing into the xref folder with its references
            try
            {
                doc.Database.SaveAs(
                    (Path.Combine(xrefloc, (Path.GetFileName(db.Filename)))),
                    true,
                    DwgVersion.AC1800,
                    db.SecurityParameters);
            }

            catch (global::Autodesk.AutoCAD.Runtime.Exception aex)
            {
                if (aex.ErrorStatus == ErrorStatus.FileAccessErr)
                {
                    // error handling code
                }
            }

            catch
            {
                // error handling code
            }
            #endregion
            #region Xref Insertion & Layout Creation
            foreach (Layout l in listoflayouts)
            {
                if (!l.ModelType)
                {
                    //create a new dwg based on the BR+A template
                    //switch the active drawing to the newly made drawing
                    string   BRA    = "G:\\Shared\\CADD\\ACAD2011\\Template\\BR+A.dwt";
                    Document newdoc = doc.CreateNew(BRA, setuploc, l.LayoutName);
                    global::Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = newdoc;
                    HostApplicationServices.WorkingDatabase = newdoc.Database;
                    try
                    {
                        global::Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CLAYER", "x-xref");
                        newdoc.LayerManagement("Lock", "x-xref", false);
                    }
                    catch
                    {
                        // error handling code
                    }

                    //Insert the consultant drawing at 0,0,0
                    newdoc.Xref_Attach(
                        (doc.Name),
                        Path.GetFileNameWithoutExtension(doc.Name),
                        "Model");

                    newdoc.Editor.Regen();

                    //insert the default titleblock based on the size of the previous document's layout
                    string tblksize;
                    int    numoflays = 0;
                    string tblk_dir  = Path.GetDirectoryName("G:\\Shared\\CADD\\BLOCKLIB\\BR&A\\NYC\\BR+A-NY.dwg");
                    string TBLK      = Path.Combine(xrefloc, "TBLK.dwg");

                    using (DocumentLock d_lock = newdoc.LockDocument())
                    {
                        using (Transaction tr1 = newdoc.Database.TransactionManager.StartTransaction())
                        {
                            numoflays++;
                            tblksize = l.PaperSize();

                            // If the layout doesn't already exist in the drawing, create it
                            LayoutManager newlaymgr         = LayoutManager.Current;
                            List <Layout> new_listoflayouts = Project_ListOfLayouts(newdoc);
                            Layout        curlay            = new Layout();
                            PlotPaperUnit ppunits           = PlotPaperUnit.Inches;
                            if (!new_listoflayouts.Any(ln => ln.LayoutName.Equals(l.LayoutName)))
                            {
                                curlay = newdoc.Create_NewLayout(l.LayoutName);
                            }

                            else
                            {
                                curlay = new_listoflayouts[new_listoflayouts.FindIndex(ln => ln.LayoutName.Equals(l.LayoutName))];
                            }
                            #endregion
                            #region Title Block Switch Statements
                            // case statements pick out specific titleblocks
                            try
                            {
                                switch (tblksize)
                                {
                                case "11,17":
                                    //change the actual page size
                                    curlay.SetPageSize(11.00, 17.00, ppunits);

                                    // save the titleblock from the G Drive into the project folder
                                    // as TBLK.dwg and the titleblock info as well as TBLKinfo.dwg
                                    if (!File.Exists(TBLK))
                                    {
                                        File.Copy((Path.Combine(tblk_dir, "11X17.dwg")), (Path.Combine(xrefloc, "TBLK.dwg")), false);
                                        File.Copy((Path.Combine(tblk_dir, "11X17INF.dwg")), (Path.Combine(xrefloc, "TBLKinfo.dwg")), false);

                                        //insert the titleblock
                                        newdoc.Xref_Attach(TBLK, "TBLK", l.LayoutName);
                                    }

                                    else
                                    {
                                        string other_tblk = Path.Combine(xrefloc, "TBLK_11X17.dwg");
                                        File.Copy((Path.Combine(tblk_dir, "11X17.dwg")), (Path.Combine(xrefloc, "TBLK_11X17.dwg")), false);
                                        File.Copy((Path.Combine(tblk_dir, "11X17INF.dwg")), (Path.Combine(xrefloc, "TBLKinfo_11X17.dwg")), false);

                                        //insert the titleblock
                                        newdoc.Xref_Attach(other_tblk, "TBLK_11X17", l.LayoutName);
                                    }
                                    break;

                                case "24,36":
                                    //change the actual page size
                                    curlay.SetPageSize(24.00, 36.00, ppunits);

                                    // save the titleblock from the G Drive into the project folder
                                    // as TBLK.dwg and the titleblock info as well as TBLKinfo.dwg
                                    if (!File.Exists(TBLK))
                                    {
                                        File.Copy((Path.Combine(tblk_dir, "24X36.dwg")), (Path.Combine(xrefloc, "TBLK.dwg")), false);
                                        File.Copy((Path.Combine(tblk_dir, "24X36INF.dwg")), (Path.Combine(xrefloc, "TBLKinfo.dwg")), false);

                                        //insert the titleblock
                                        newdoc.Xref_Attach(TBLK, "TBLK", l.LayoutName);
                                    }

                                    else
                                    {
                                        string other_tblk = Path.Combine(xrefloc, "TBLK_24X36.dwg");
                                        File.Copy((Path.Combine(tblk_dir, "24X36.dwg")), (Path.Combine(xrefloc, "TBLK_24X36.dwg")), false);
                                        File.Copy((Path.Combine(tblk_dir, "24X36INF.dwg")), (Path.Combine(xrefloc, "TBLKinfo_24X36.dwg")), false);

                                        //insert the titleblock
                                        newdoc.Xref_Attach(other_tblk, "TBLK_24X36", l.LayoutName);
                                    }
                                    break;

                                case "30,42":
                                    //change the actual page size
                                    curlay.SetPageSize(30.00, 42.00, ppunits);

                                    // save the titleblock from the G Drive into the project folder
                                    // as TBLK.dwg and the titleblock info as well as TBLKinfo.dwg
                                    if (!File.Exists(TBLK))
                                    {
                                        File.Copy((Path.Combine(tblk_dir, "30X42.dwg")), (Path.Combine(xrefloc, "TBLK.dwg")), false);
                                        File.Copy((Path.Combine(tblk_dir, "30X42INF.dwg")), (Path.Combine(xrefloc, "TBLKinfo.dwg")), false);

                                        //insert the titleblock
                                        newdoc.Xref_Attach(TBLK, "TBLK", l.LayoutName);
                                    }

                                    else
                                    {
                                        string other_tblk = Path.Combine(xrefloc, "TBLK_30X42.dwg");
                                        File.Copy((Path.Combine(tblk_dir, "30X42.dwg")), (Path.Combine(xrefloc, "TBLK_30X42.dwg")), false);
                                        File.Copy((Path.Combine(tblk_dir, "30X42INF.dwg")), (Path.Combine(xrefloc, "TBLKinfo_30X42.dwg")), false);

                                        //insert the titleblock
                                        newdoc.Xref_Attach(other_tblk, "TBLK_30X42", l.LayoutName);
                                    }
                                    break;

                                case "30,48":
                                    //change the actual page size
                                    curlay.SetPageSize(30.00, 48.00, ppunits);

                                    // save the titleblock from the G Drive into the project folder
                                    // as TBLK.dwg and the titleblock info as well as TBLKinfo.dwg
                                    if (!File.Exists(TBLK))
                                    {
                                        File.Copy((Path.Combine(tblk_dir, "30X48.dwg")), (Path.Combine(xrefloc, "TBLK.dwg")), false);
                                        File.Copy((Path.Combine(tblk_dir, "30X48INF.dwg")), (Path.Combine(xrefloc, "TBLKinfo.dwg")), false);

                                        //insert the titleblock
                                        newdoc.Xref_Attach(TBLK, "TBLK", l.LayoutName);
                                    }

                                    else
                                    {
                                        string other_tblk = Path.Combine(xrefloc, "TBLK_30X48.dwg");
                                        File.Copy((Path.Combine(tblk_dir, "30X48.dwg")), (Path.Combine(xrefloc, "TBLK_30X48.dwg")), false);
                                        File.Copy((Path.Combine(tblk_dir, "30X48INF.dwg")), (Path.Combine(xrefloc, "TBLKinfo_30X48.dwg")), false);

                                        //insert the titleblock
                                        newdoc.Xref_Attach(other_tblk, "TBLK_30X48", l.LayoutName);
                                    }
                                    break;

                                default:
                                case "36,48":
                                    //change the actual page size
                                    curlay.SetPageSize(36.00, 48.00, ppunits);

                                    // save the titleblock from the G Drive into the project folder
                                    // as TBLK.dwg and the titleblock info as well as TBLKinfo.dwg
                                    if (!File.Exists(TBLK))
                                    {
                                        File.Copy((Path.Combine(tblk_dir, "36X48.dwg")), (Path.Combine(xrefloc, "TBLK.dwg")), false);
                                        File.Copy((Path.Combine(tblk_dir, "36X48INF.dwg")), (Path.Combine(xrefloc, "TBLKinfo.dwg")), false);

                                        //insert the titleblock
                                        newdoc.Xref_Attach(TBLK, "TBLK", l.LayoutName);
                                    }

                                    else
                                    {
                                        string other_tblk = Path.Combine(xrefloc, "TBLK_36X48.dwg");
                                        File.Copy((Path.Combine(tblk_dir, "36X48.dwg")), (Path.Combine(xrefloc, "TBLK_36X48.dwg")), false);
                                        File.Copy((Path.Combine(tblk_dir, "36X48INF.dwg")), (Path.Combine(xrefloc, "TBLKinfo_36X48.dwg")), false);

                                        //insert the titleblock
                                        newdoc.Xref_Attach(other_tblk, "TBLK_36X48", l.LayoutName);
                                    }
                                    break;
                                }
                            }

                            catch (IOException)
                            {
                                //change the actual page size
                                curlay.SetPageSize(36.00, 48.00, ppunits);

                                //insert the titleblock
                                newdoc.Xref_Attach(TBLK, "TBLK", l.LayoutName);
                            }
                            //newdoc.Editor.Regen();

                            tr1.Commit();
                        }
                        #endregion
                        #region Layout Creation

                        //switches to appropiate layout and calls it "Work"
                        using (Transaction tr2 = mgr_doc.MdiActiveDocument.Database.TransactionManager.StartTransaction())
                        {
                            LayoutManager newlaymgr = LayoutManager.Current;
                            Layout        curlay    = new Layout();

                            List <Layout> new_listoflayouts = Project_ListOfLayouts(newdoc);
                            if (new_listoflayouts.Any(ln => ln.LayoutName.Equals(l.LayoutName)))
                            {
                                for (int i = 0; i < new_listoflayouts.Count; i++)
                                {
                                    if (new_listoflayouts[i].LayoutName.Equals(l.LayoutName))
                                    {
                                        curlay = new_listoflayouts[i];
                                        break;
                                    }
                                }
                            }

                            else
                            {
                                newdoc.Create_NewLayout(l.LayoutName);
                                for (int i = 0; i < new_listoflayouts.Count; i++)
                                {
                                    if (new_listoflayouts[i].LayoutName.Equals(l.LayoutName))
                                    {
                                        curlay = new_listoflayouts[i];
                                        break;
                                    }
                                }
                            }

                            if (curlay.LayoutName.ToUpper() != "LAYOUT1")
                            {
                                newlaymgr.DeleteLayout("Layout1");
                            }

                            newlaymgr.RenameLayout(curlay.LayoutName, "Work");

                            // for each set of viewport info in the List of VP info
                            foreach (AutoCADViewport vpinfo in vp_dict[l])
                            {
                                // create a new vp with the corresponding info
                                curlay.Viewport_Create(vpinfo, newdoc);
                                //newdoc.Editor.Regen();
                            }
                            tr2.Commit();
                        }
                        newdoc.Editor.Regen();

                        //delete the extra viewport that gets created sometimes...
                        using (Transaction tr3 = newdoc.TransactionManager.StartTransaction())
                        {
                            LayoutManager    newlaymgr1 = LayoutManager.Current;
                            Layout           layout     = tr3.GetObject(newlaymgr1.GetLayoutId(newlaymgr1.CurrentLayout), OpenMode.ForWrite) as Layout;
                            BlockTableRecord lay_btr    = tr3.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite) as BlockTableRecord;

                            ObjectIdCollection vp_ids  = layout.GetViewports();
                            RXClass            VPClass = RXObject.GetClass(typeof(Viewport));
                            foreach (ObjectId item in lay_btr)
                            {
                                if (item.ObjectClass == VPClass)
                                {
                                    Viewport vp        = tr3.GetObject(item, OpenMode.ForWrite) as Viewport;
                                    var      laywidth  = layout.GetWidth();
                                    var      layheight = layout.GetHeight();
                                    if (vp.Number != 1 && (vp.Height > layheight || vp.Width > laywidth))
                                    {
                                        vp.Erase();
                                    }
                                    newdoc.Editor.Regen();
                                }
                            }
                            tr3.Commit();
                        }
                    }
                    #endregion
                    #region Closing Area
                    try
                    {
                        AutoCADCommand.Audit();
                        AutoCADCommand.Purge();

                        //save all the changes
                        newdoc.Database.SaveAs(
                            (newdoc.Name),
                            true,
                            DwgVersion.AC1800,
                            db.SecurityParameters);
                    }

                    catch
                    {
                        break;
                    }

                    try
                    {
                        mgr_doc.MdiActiveDocument.CloseAndDiscard();
                    }

                    catch
                    { }
                }
            }
            try
            {
                //mgr_doc.MdiActiveDocument = mgr_doc.GetDocument(db);
                //mgr_doc.GetDocument(db).CloseAndDiscard();
                //mgr_doc.Open("G:\\BRATEMP\\PDS\\BLANK.DWG",false);
            }

            catch (System.Runtime.InteropServices.COMException acadcom)
            {
                Console.WriteLine("Exception was {0} type and the inner exception was {1}", acadcom.ErrorCode, acadcom.InnerException);
                Console.ReadLine();
            }
        }