Пример #1
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);
            }
        }
Пример #2
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();
                }
            }
        }