zoukankan      html  css  js  c++  java
  • AutoCAD.Net/C#.Net QQ群:193522571 previewicon生成的块图标太小,CMLContentSearchPreviews生成大的图片

    由于CMLContentSearchPreviews方法是AutoCAD2014中才加入的,所以只能应用于2014及以后版本,可惜啊!

    using System.IO;
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.Windows.Data;
    namespace BlockPreviews
    {
        public class Commands
        {
            [CommandMethod("GBP", CommandFlags.Session)]
            public static void GenerateBlockPreviews()
            {
                var ed = Application.DocumentManager.MdiActiveDocument.Editor;
                var res = ed.GetFileNameForOpen("Select file for which to generate previews");
                if (res.Status != PromptStatus.OK)
                    return;
                string dwgname = res.StringResult;
                Document doc = null;
                try
                {
                    doc = Application.DocumentManager.Open(dwgname, false);
                }
                catch
                {
                    ed.WriteMessage("
    Unable to read drawing.");
                    return;
                }
                var db = doc.Database;
                string path = Path.GetDirectoryName(dwgname),
                       name = Path.GetFileName(dwgname),
                       iconPath = path + "\" + name + " icons";
                int numIcons = 0;
                using (var tr = doc.TransactionManager.StartTransaction())
                {
                    var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
    
                    foreach (ObjectId btrId in bt)
                    {
                        var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
                        // Ignore layouts and anonymous blocks
                        if (btr.IsLayout || btr.IsAnonymous)
                            continue;
                        // Attempt to generate an icon, where one doesn't exist
                        if (btr.PreviewIcon == null)
                        {
                            object ActiveDocument = doc.GetAcadDocument();
                            object[] data = { "_.BLOCKICON " + btr.Name + "
    " };
                            ActiveDocument.GetType().InvokeMember(
                              "SendCommand",
                              System.Reflection.BindingFlags.InvokeMethod,
                              null, ActiveDocument, data
                            );
                        }
    
                        // Hopefully we now have an icon
    
                        if (btr.PreviewIcon != null)
                        {
                            // Create the output directory, if it isn't yet there
    
                            if (!Directory.Exists(iconPath))
                                Directory.CreateDirectory(iconPath);
    
                            var fname = iconPath + "\" + btr.Name + ".bmp";
    
                            // Delete the image if it already exists
    
                            if (File.Exists(fname))
                                File.Delete(fname);
    
                            // Save the icon to our out directory
    
                            btr.PreviewIcon.Save(fname);
    
                            // Increment our icon counter
    
                            numIcons++;
                        }
                    }
                    tr.Commit();
                }
                doc.CloseAndDiscard();
    
                ed.WriteMessage("
    {0} block icons saved to "{1}".", numIcons, iconPath);
            }
    
            [CommandMethod("GBP2")]
            public static void GenerateBlockPreviews2()
            {
                Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
    
                PromptFileNameResult res = ed.GetFileNameForOpen("Select file for which to generate previews");
                if (res.Status != PromptStatus.OK)
                    return;
    
                string dwgname = res.StringResult;
                int numIcons;
                string iconPath;
    
                // We don't need a document for access to the BlockTable
    
                using (var db = new Database(false, true))
                {
                    try
                    {
                        db.ReadDwgFile(
                          dwgname,
                          FileOpenMode.OpenForReadAndReadShare,
                          true,
                          ""
                        );
                    }
                    catch
                    {
                        ed.WriteMessage("
    Unable to read drawing.");
                        return;
                    }
    
                    var path = Path.GetDirectoryName(dwgname);
                    var name = Path.GetFileName(dwgname);
                    iconPath = path + "\" + name + " icons";
    
                    using (var tr = db.TransactionManager.StartTransaction())
                    {
                        var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                        numIcons = ExtractThumbnails(iconPath, tr, bt);
                        tr.Commit();
                    }
                }
    
                ed.WriteMessage("
    {0} block icons saved to "{1}".", numIcons, iconPath);
            }
    
            [CommandMethod("GBPC")]
            public static void GenerateCurrentBlockPreviews()
            {
                var doc = Application.DocumentManager.MdiActiveDocument;
                var ed = doc.Editor;
                var db = doc.Database;
    
                var path = (string)Application.GetSystemVariable("DWGPREFIX");
                var name = (string)Application.GetSystemVariable("DWGNAME");
                var iconPath = path + name + " icons";
    
                using (var tr = doc.TransactionManager.StartTransaction())
                {
                    var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    int numIcons = ExtractThumbnails(iconPath, tr, bt);
                    tr.Commit();
                    ed.WriteMessage("
    {0} block icons saved to "{1}".", numIcons, iconPath);
                }
            }
    
            private static int ExtractThumbnails(string iconPath, Transaction tr, BlockTable bt)
            {
                int numIcons = 0;
                foreach (ObjectId btrId in bt)
                {
                    var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
    
                    // Ignore layouts and anonymous blocks
                    if (btr.IsLayout || btr.IsAnonymous)
                        continue;
                    // Attempt to generate an icon, where one doesn't exist
                    {
                        // Create the output directory, if it isn't yet there
                        if (!Directory.Exists(iconPath))
                            Directory.CreateDirectory(iconPath);
                        // Save the icon to our out directory
                        var imgsrc = CMLContentSearchPreviews.GetBlockTRThumbnail(btr);
                        var bmp = ImageSourceToGDI(imgsrc as System.Windows.Media.Imaging.BitmapSource);
                        var fname = iconPath + "\" + btr.Name + ".bmp";
                        if (File.Exists(fname))
                            File.Delete(fname);
                        bmp.Save(fname);
                        // Increment our icon counter
                        numIcons++;
                    }
                }
                return numIcons;
            }
    
            // Helper function to generate an Image from a BitmapSource
    
            private static System.Drawing.Image ImageSourceToGDI(System.Windows.Media.Imaging.BitmapSource src
            )
            {
                var ms = new MemoryStream();
                var encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(src));
                encoder.Save(ms);
                ms.Flush();
                return System.Drawing.Image.FromStream(ms);
            }
        }
    }
  • 相关阅读:
    QWT编译与配置-Windows/Linux环境
    Manjaro Linux KDE个人的一些安装配置
    2019嵌入式之路的反思
    Linux内核调用I2C驱动_驱动嵌套驱动方法
    Ubuntu Linux TinySerial串口调试助手 可视化界面 安装使用
    ZYNQ的Linux Linaro系统镜像制作SD卡启动
    eclipse CDT Error: Program "g++" not found in PATH
    js Date格式化时间兼容写法
    ajax多图片上传demo
    php文件上传$_FILES数组格式
  • 原文地址:https://www.cnblogs.com/swtool/p/3764774.html
Copyright © 2011-2022 走看看