示例#1
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());
        }
        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();
                }
            }
        }