Exemplo n.º 1
0
        private static bool SetPlotterInLayouts()
        {
            var ok = true;

            using (_Tr = _Db.TransactionManager.StartTransaction())
            {
                var layManager = _AcDb.LayoutManager.Current;
                var plotSetVal = _AcDb.PlotSettingsValidator.Current;

                //_AcDb.DBDictionary layoutsEx = acTransEx.GetObject(acExDb.LayoutDictionaryId, _AcDb.OpenMode.ForRead) as _AcDb.DBDictionary;
                var layouts = _Tr.GetObject(_Db.LayoutDictionaryId, _AcDb.OpenMode.ForRead) as _AcDb.DBDictionary;
                foreach (var layoutDe in layouts)
                {
                    //_AcDb.ObjectId layoutId = layManager.GetLayoutId(layManager.CurrentLayout);
                    var layoutId  = layoutDe.Value;
                    var layoutObj = (_AcDb.Layout)_Tr.GetObject(layoutId, _AcDb.OpenMode.ForWrite);
                    //ed.WriteMessage("Style sheet of current layout: " + layoutObj.CurrentStyleSheet + "\n");
                    plotSetVal.RefreshLists(layoutObj);
                    System.Collections.Specialized.StringCollection deviceList = plotSetVal.GetPlotDeviceList();
                    bool plotterExists = PlotterExists(deviceList);
                    if (!plotterExists)
                    {
                        ok = false;
                        break;
                    }
                    else
                    {
                        using (var ps = new _AcDb.PlotSettings(layoutObj.ModelType))
                        {
                            ps.CopyFrom(layoutObj);
                            if (_NoPlotterInModelSpace && layoutObj.LayoutName == "Model")
                            {
                                plotSetVal.SetPlotConfigurationName(ps, _NoPlotterName, null);
                            }
                            else
                            {
                                plotSetVal.SetPlotConfigurationName(ps, _PlotterName, null);
                            }
                            layoutObj.CopyFrom(ps);
                        }
                        _NrPlotters++;
                    }
                }
                _Tr.Commit();
            }
            return(ok);
        }
Exemplo n.º 2
0
        private void setLayoutPlotSettings(_Db.Layout lay, string device, string pageSize, string styleSheet)
        {
            using (_Db.PlotSettings plotSettings = new _Db.PlotSettings(lay.ModelType))
            {
                plotSettings.CopyFrom(lay);
                _Db.PlotSettingsValidator validator = _Db.PlotSettingsValidator.Current;

                StringCollection devices = validator.GetPlotDeviceList();
                if (devices.Contains(device))
                {
                    validator.SetPlotConfigurationName(plotSettings, device, null);
                    validator.RefreshLists(plotSettings);
                }
                else
                {
                    write("[WARNING] Device not found!");
                }

                StringCollection paperSizes = validator.GetCanonicalMediaNameList(plotSettings);
                if (paperSizes.Contains(pageSize))
                {
                    validator.SetCanonicalMediaName(plotSettings, pageSize);
                }
                else
                {
                    write("[WARNING] Paper not found!");
                }

                StringCollection styleSheets = validator.GetPlotStyleSheetList();
                if (styleSheets.Contains(styleSheet))
                {
                    validator.SetCurrentStyleSheet(plotSettings, styleSheet);
                }
                else
                {
                    write("[WARNING] Style not found!");
                }

                lay.CopyFrom(plotSettings);
            }
        }
Exemplo n.º 3
0
        public void PlotLayout(_Db.Layout lay, string location)
        {
            using (_Pl.PlotInfo plotInfo = new _Pl.PlotInfo())
            {
                plotInfo.Layout = lay.ObjectId;

                using (_Db.PlotSettings plotSettings = new _Db.PlotSettings(lay.ModelType))
                {
                    plotSettings.CopyFrom(lay);
                    plotInfo.OverrideSettings = plotSettings;

                    _Db.PlotSettingsValidator plotValidator = _Db.PlotSettingsValidator.Current;

                    using (_Pl.PlotInfoValidator infoValidator = new _Pl.PlotInfoValidator())
                    {
                        infoValidator.MediaMatchingPolicy = _Pl.MatchingPolicy.MatchEnabled;
                        infoValidator.Validate(plotInfo);

                        using (_Pl.PlotProgressDialog dialog = new _Pl.PlotProgressDialog(false, 1, true))
                        {
                            write("Plotting: " + _c.doc.Name + " - " + lay.LayoutName);

                            engine.BeginPlot(dialog, null);
                            engine.BeginDocument(plotInfo, _c.doc.Name, null, 1, true, location);
                            using (_Pl.PlotPageInfo pageInfo = new _Pl.PlotPageInfo())
                            {
                                engine.BeginPage(pageInfo, plotInfo, true, null);
                            }
                            engine.BeginGenerateGraphics(null);

                            engine.EndGenerateGraphics(null);
                            engine.EndPage(null);
                            engine.EndDocument(null);
                            engine.EndPlot(null);
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        private static void SetClosestMediaName(_AcDb.PlotSettingsValidator psv, string device, _AcDb.PlotSettings ps, double pageWidth, double pageHeight, _AcDb.PlotPaperUnit units, bool matchPrintableArea)
        {
            psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
            psv.SetPlotPaperUnits(ps, units);
            psv.SetUseStandardScale(ps, false);
            psv.SetStdScaleType(ps, _AcDb.StdScaleType.ScaleToFit);
            psv.SetPlotConfigurationName(ps, device, null);
            psv.RefreshLists(ps);
            StringCollection mediaList      = psv.GetCanonicalMediaNameList(ps);
            double           smallestOffset = 0.0;
            string           selectedMedia  = string.Empty;

            _AcDb.PlotRotation selectedRot = _AcDb.PlotRotation.Degrees000;
            foreach (string media in mediaList)
            {
                psv.SetCanonicalMediaName(ps, media);
                psv.SetPlotPaperUnits(ps, units);
                double mediaPageWidth  = ps.PlotPaperSize.X;
                double mediaPageHeight = ps.PlotPaperSize.Y;
                if (matchPrintableArea)
                {
                    mediaPageWidth  -= (ps.PlotPaperMargins.MinPoint.X + ps.PlotPaperMargins.MaxPoint.X);
                    mediaPageHeight -= (ps.PlotPaperMargins.MinPoint.Y + ps.PlotPaperMargins.MaxPoint.Y);
                }

                _AcDb.PlotRotation rotationType = _AcDb.PlotRotation.Degrees090;
                //Check that we are not outside the media print area
                if (mediaPageWidth < pageWidth || mediaPageHeight < pageHeight)
                {
                    //Check if 90°Rot will fit, otherwise check next media
                    if (mediaPageHeight < pageWidth || mediaPageWidth >= pageHeight)
                    {
                        //Too small, let's check next media
                        continue;
                    }
                    //That's ok 90°Rot will fit
                    rotationType = _AcDb.PlotRotation.Degrees090;
                }

                double offset = Math.Abs(mediaPageWidth * mediaPageHeight - pageWidth * pageHeight);
                if (selectedMedia == string.Empty || offset < smallestOffset)
                {
                    selectedMedia  = media;
                    smallestOffset = offset;
                    selectedRot    = rotationType;
                    //Found perfect match so we can quit early
                    if (smallestOffset == 0)
                    {
                        break;
                    }
                }
            }

            psv.SetCanonicalMediaName(ps, selectedMedia);
            psv.SetPlotRotation(ps, selectedRot);
            string localMedia = psv.GetLocaleMediaName(ps, selectedMedia);

            _Editor.WriteMessage("\n - Closest Media: " + localMedia);
            _Editor.WriteMessage("\n - Offset: " + smallestOffset.ToString());
            _Editor.WriteMessage("\n - Rotation: " + selectedRot.ToString());
        }
Exemplo n.º 5
0
        private static bool PlotDwf(_AcDb.Layout lo, string dwfName)
        {
            try
            {
                var logMsg = string.Format("Erzeuge DWF-Datei für Layout '{0}'.", lo.LayoutName);
                log.Info(logMsg);
                _Editor.WriteMessage("\n" + logMsg);

                if (File.Exists(dwfName))
                {
                    File.Delete(dwfName);
                }

                var pi = new _AcPl.PlotInfo();
                pi.Layout = lo.Id;
                var ps = new _AcDb.PlotSettings(lo.ModelType);
                ps.CopyFrom(lo);
                var psv = _AcDb.PlotSettingsValidator.Current;
                psv.SetPlotConfigurationName(ps, _PlotterName, null);
                SetClosestMediaName(psv, _PlotterName, ps, lo.PlotPaperSize.X, lo.PlotPaperSize.Y, _AcDb.PlotPaperUnit.Millimeters, true);
                psv.SetPlotCentered(ps, true);

                pi.OverrideSettings = ps;
                var piv = new _AcPl.PlotInfoValidator();
                piv.MediaMatchingPolicy = _AcPl.MatchingPolicy.MatchEnabled;
                piv.Validate(pi);
                // You probably need to make sure background plotting is disabled. If the BACKGROUNDPLOT sysvar is set to 1 or 3, then PLOT will background plot (which is not what you want).
                // A PlotEngine does the actual plotting
                // (can also create one for Preview)
                if (_AcPl.PlotFactory.ProcessPlotState == _AcPl.ProcessPlotState.NotPlotting)
                {
                    var pe = _AcPl.PlotFactory.CreatePublishEngine();
                    using (pe)
                    {
                        // Create a Progress Dialog to provide info
                        // and allow the user to cancel
                        var ppd = new _AcPl.PlotProgressDialog(false, 1, true);
                        using (ppd)
                        {
                            ppd.set_PlotMsgString(_AcPl.PlotMessageIndex.DialogTitle, "Plot Fortschritt");
                            ppd.set_PlotMsgString(_AcPl.PlotMessageIndex.CancelJobButtonMessage, "Plot Abbrechen");
                            ppd.set_PlotMsgString(_AcPl.PlotMessageIndex.CancelSheetButtonMessage, "Sheet Abbrechen");
                            ppd.set_PlotMsgString(_AcPl.PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Fortschritt");
                            ppd.set_PlotMsgString(_AcPl.PlotMessageIndex.SheetProgressCaption, "Sheet Fortschritt");
                            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
                            pe.BeginDocument(pi, _Doc.Name, null, 1, true, dwfName);
                            // Which contains a single sheet
                            ppd.OnBeginSheet();
                            ppd.LowerSheetProgressRange = 0;
                            ppd.UpperSheetProgressRange = 100;
                            ppd.SheetProgressPos        = 0;
                            var ppi = new _AcPl.PlotPageInfo();
                            pe.BeginPage(ppi, pi, true, null);
                            pe.BeginGenerateGraphics(null);
                            pe.EndGenerateGraphics(null);

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

                            // Finish the document
                            pe.EndDocument(null);

                            // And finish the plot
                            ppd.PlotProgressPos = 100;
                            ppd.OnEndPlot();
                            pe.EndPlot(null);
                        }
                    }
                }
                else
                {
                    var msg = string.Format("Fehler beim Plot von Layout '{0}' in Zeichnung '{1}'! Es wird gerade geplottet!", lo.LayoutName, _Doc.Name);
                    log.Warn(msg);
                    _Editor.WriteMessage("\n" + msg);
                    return(false);
                }
            }
            catch (Exception ex)
            {
                var msg = string.Format("Fehler beim Plot von Layout '{0}' in Zeichnung '{1}'! {2}", lo.LayoutName, _Doc.Name, ex.Message);
                log.Warn(msg);
                _Editor.WriteMessage("\n" + msg);
                return(false);
            }
            _NrPlots++;
            return(true);
        }
Exemplo n.º 6
0
        public static void ImportLayout(string layoutName, string filename)
        {
            // Get the current document and database
            _AcAp.Document acDoc   = _AcAp.Application.DocumentManager.MdiActiveDocument;
            _AcDb.Database acCurDb = acDoc.Database;

            // Specify the layout name and drawing file to work with
            //string layoutName = "MAIN AND SECOND FLOOR PLAN";
            //string filename = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\A-01.dwg";

            // Create a new database object and open the drawing into memory
            _AcDb.Database acExDb = new _AcDb.Database(false, true);
            acExDb.ReadDwgFile(filename, Autodesk.AutoCAD.DatabaseServices.FileOpenMode.OpenForReadAndAllShare, true, "");

            // Create a transaction for the external drawing
            using (_AcDb.Transaction acTransEx = acExDb.TransactionManager.StartTransaction())
            {
                // Get the layouts dictionary
                _AcDb.DBDictionary layoutsEx = acTransEx.GetObject(acExDb.LayoutDictionaryId, _AcDb.OpenMode.ForRead) as _AcDb.DBDictionary;

                // Check to see if the layout exists in the external drawing
                if (layoutsEx.Contains(layoutName) == true)
                {
                    // Get the layout and block objects from the external drawing
                    _AcDb.Layout           layEx       = layoutsEx.GetAt(layoutName).GetObject(_AcDb.OpenMode.ForRead) as _AcDb.Layout;
                    _AcDb.BlockTableRecord blkBlkRecEx = acTransEx.GetObject(layEx.BlockTableRecordId, _AcDb.OpenMode.ForRead) as _AcDb.BlockTableRecord;

                    // Get the objects from the block associated with the layout
                    _AcDb.ObjectIdCollection idCol = new _AcDb.ObjectIdCollection();
                    foreach (_AcDb.ObjectId id in blkBlkRecEx)
                    {
                        idCol.Add(id);
                    }

                    // Create a transaction for the current drawing
                    using (_AcDb.Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                    {
                        // Get the block table and create a new block
                        // then copy the objects between drawings
                        _AcDb.BlockTable blkTbl = acTrans.GetObject(acCurDb.BlockTableId, _AcDb.OpenMode.ForWrite) as _AcDb.BlockTable;
                        using (_AcDb.BlockTableRecord blkBlkRec = new _AcDb.BlockTableRecord())
                        {
                            int layoutCount = layoutsEx.Count - 1;

                            blkBlkRec.Name = "*Paper_Space" + layoutCount.ToString();
                            blkTbl.Add(blkBlkRec);
                            acTrans.AddNewlyCreatedDBObject(blkBlkRec, true);
                            acExDb.WblockCloneObjects(idCol, blkBlkRec.ObjectId, new _AcDb.IdMapping(), _AcDb.DuplicateRecordCloning.Ignore, false);

                            // Create a new layout and then copy properties between drawings
                            _AcDb.DBDictionary layouts = acTrans.GetObject(acCurDb.LayoutDictionaryId, _AcDb.OpenMode.ForWrite) as _AcDb.DBDictionary;
                            using (_AcDb.Layout lay = new _AcDb.Layout())
                            {
                                lay.LayoutName = layoutName;
                                lay.AddToLayoutDictionary(acCurDb, blkBlkRec.ObjectId);
                                acTrans.AddNewlyCreatedDBObject(lay, true);
                                lay.CopyFrom(layEx);

                                _AcDb.DBDictionary plSets = acTrans.GetObject(acCurDb.PlotSettingsDictionaryId, _AcDb.OpenMode.ForRead) as _AcDb.DBDictionary;

                                // Check to see if a named page setup was assigned to the layout,
                                // if so then copy the page setup settings
                                if (lay.PlotSettingsName != "")
                                {
                                    // Check to see if the page setup exists
                                    if (plSets.Contains(lay.PlotSettingsName) == false)
                                    {
                                        plSets.UpgradeOpen();

                                        using (_AcDb.PlotSettings plSet = new _AcDb.PlotSettings(lay.ModelType))
                                        {
                                            plSet.PlotSettingsName = lay.PlotSettingsName;
                                            plSet.AddToPlotSettingsDictionary(acCurDb);
                                            acTrans.AddNewlyCreatedDBObject(plSet, true);

                                            _AcDb.DBDictionary plSetsEx = acTransEx.GetObject(acExDb.PlotSettingsDictionaryId, _AcDb.OpenMode.ForRead) as _AcDb.DBDictionary;

                                            _AcDb.PlotSettings plSetEx = plSetsEx.GetAt(lay.PlotSettingsName).GetObject(_AcDb.OpenMode.ForRead) as _AcDb.PlotSettings;

                                            plSet.CopyFrom(plSetEx);
                                        }
                                    }
                                }
                            }
                        }

                        // Regen the drawing to get the layout tab to display
                        acDoc.Editor.Regen();

                        // Save the changes made
                        acTrans.Commit();
                    }
                }
                else
                {
                    // Display a message if the layout could not be found in the specified drawing
                    acDoc.Editor.WriteMessage("\nLayout '" + layoutName +
                                              "' could not be imported from '" + filename + "'.");
                }

                // Discard the changes made to the external drawing file
                acTransEx.Abort();
            }

            // Close the external drawing file
            acExDb.Dispose();
        }
        public static void SetPlotSettings(this _AcDb.Layout lay, string device, string mediaName, string styleSheet, double?scaleNumerator, double?scaleDenominator, short?rotation)
        {
            using (var ps = new _AcDb.PlotSettings(lay.ModelType))
            {
                ps.CopyFrom(lay);

                var psv = _AcDb.PlotSettingsValidator.Current;

                // Set the device
                if (!string.IsNullOrEmpty(device))
                {
                    var devs = psv.GetPlotDeviceList();

                    if (devs.ContainsIgnoreUc(ref device))
                    {
                        log.InfoFormat(CultureInfo.CurrentCulture, "Setze Device '{0}' für Layout '{1}'.", device, lay.LayoutName);
                        psv.SetPlotConfigurationName(ps, device, null);
                        psv.RefreshLists(ps);
                    }
                    else
                    {
                        log.WarnFormat(CultureInfo.CurrentCulture, "Device '{0}' existiert nicht!", device);
                    }
                }

                // Set the media name/size

                if (!string.IsNullOrEmpty(mediaName))
                {
                    var mns = psv.GetCanonicalMediaNameList(ps);
                    if (mns.ContainsIgnoreUc(ref mediaName))
                    {
                        log.InfoFormat(CultureInfo.CurrentCulture, "Setze PageSize '{0}' für Layout '{1}'.", mediaName, lay.LayoutName);
                        psv.SetCanonicalMediaName(ps, mediaName);
                    }
                    else
                    {
                        string canonicalMediaName = LocaleToCanonicalMediaName(psv, ps, mediaName);
                        if (!string.IsNullOrEmpty(canonicalMediaName))
                        {
                            log.InfoFormat(CultureInfo.CurrentCulture, "Setze PageSize '{0}' für Layout '{1}'.", canonicalMediaName, lay.LayoutName);
                            psv.SetCanonicalMediaName(ps, canonicalMediaName);
                        }
                        else
                        {
                            log.WarnFormat(CultureInfo.CurrentCulture, "Size '{0}' existiert nicht!", mediaName);
                        }
                    }
                }

                // Set the pen settings
                if (!string.IsNullOrEmpty(styleSheet))
                {
                    var ssl = psv.GetPlotStyleSheetList();

                    if (ssl.ContainsIgnoreUc(ref styleSheet))
                    {
                        log.InfoFormat(CultureInfo.CurrentCulture, "Setze StyleSheet '{0}' für Layout '{1}'.", styleSheet, lay.LayoutName);
                        psv.SetCurrentStyleSheet(ps, styleSheet);
                    }
                    else
                    {
                        log.WarnFormat(CultureInfo.CurrentCulture, "Stylesheet '{0}' existiert nicht!", mediaName);
                    }
                }

                // Copy the PlotSettings data back to the Layout
                if (scaleNumerator.HasValue && scaleDenominator.HasValue)
                {
                    log.InfoFormat(CultureInfo.CurrentCulture, "Setze Scale '{0}:{2}' für Layout '{1}'.", scaleNumerator.Value.ToString(), lay.LayoutName, scaleDenominator.Value.ToString());
                    _AcDb.CustomScale cs = new _AcDb.CustomScale(scaleNumerator.Value, scaleDenominator.Value);

                    if (ps.PlotPaperUnits != _AcDb.PlotPaperUnit.Millimeters)
                    {
                        psv.SetPlotPaperUnits(ps, _AcDb.PlotPaperUnit.Millimeters);
                    }
                    psv.SetCustomPrintScale(ps, cs);
                }

                if (rotation.HasValue && (rotation.Value == 0 || rotation.Value == 90 || rotation.Value == 180 || rotation.Value == 270))
                {
                    _AcDb.PlotRotation pRot = _AcDb.PlotRotation.Degrees000;
                    switch (rotation.Value)
                    {
                    case 90:
                        pRot = _AcDb.PlotRotation.Degrees090;
                        break;

                    case 180:
                        pRot = _AcDb.PlotRotation.Degrees180;
                        break;

                    case 270:
                        pRot = _AcDb.PlotRotation.Degrees270;
                        break;

                    case 0:
                    default:
                        break;
                    }
                    log.InfoFormat(CultureInfo.CurrentCulture, "Setze Rotation '{0}' für Layout '{1}'.", pRot.ToString(), lay.LayoutName);
                    psv.SetPlotRotation(ps, pRot);
                }

                // plottype hardcoded auf fenster
                try
                {
                    if (ps.PlotType != _AcDb.PlotType.Window)
                    {
                        if (ps.PlotWindowArea.ToString() == "((0,0),(0,0))")
                        {
                            _AcDb.Extents2d e2d = new _AcDb.Extents2d(0.0, 0.0, 10.0, 10.0);
                            psv.SetPlotWindowArea(ps, e2d);
                        }
                        psv.SetPlotType(ps, _AcDb.PlotType.Window);
                    }
                }
                catch (Exception ex)
                {
                    log.ErrorFormat(CultureInfo.CurrentCulture, "Fehler beim Setzen von PlotType auf FENSTER! {0}", ex.Message);
                }

                var upgraded = false;
                if (!lay.IsWriteEnabled)
                {
                    lay.UpgradeOpen();
                    upgraded = true;
                }

                lay.CopyFrom(ps);

                if (upgraded)
                {
                    lay.DowngradeOpen();
                }
            }
        }
        private static string LocaleToCanonicalMediaName(_AcDb.PlotSettingsValidator psv, _AcDb.PlotSettings ps, string mn)
        {
            int cnt = 0;

            foreach (string mediaName in psv.GetCanonicalMediaNameList(ps))
            {
                string localeMediaName = psv.GetLocaleMediaName(ps, cnt);
                if (string.Compare(mn, localeMediaName, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return(mediaName);
                }

                cnt = cnt + 1;
            }

            return(null);
        }