田草博客

互联网田草博客


网友交流QQ群:11740834 需注明申请加入原因

微信 公众号:ByCAD

邮箱:tiancao1001x126.com
ByCAD,微信公众号
首页 | 普通 | 电脑 | AutoCAD | VB/VB.NET | FLash | 结构 | 建筑 | 电影 | BIM | 规范 | 软件 | ID
-随机-|-分布-
-博客论坛-|-﨣﨤﨧﨨-
-网站导航-|-规范下载-
-BelovedFLash欣赏-

用户登陆
用户:
密码:
 

站点日历
73 2024 - 12 48
1234567
891011121314
15161718192021
22232425262728
293031


站点统计

最新评论



都是大火惹的祸 how to get preview dwg without autocad ?
未知 AutoCAD VB.net 块管理器   [ 日期:2010-11-24 ]   [ 来自:翻译 ]  HTML
原文地址:http://www.theswamp.org/index.php?topic=35280.0


This is basically embarrassing to post compared to Daniel's My Block Manager

Hopefully this could be a start for anyone who would like to improve it and post the improvements.

But it gives the basic idea of creating a tool palette with a ListView for inserting blocks using .NET.

I threw this together real quick and since I am having problems with my computer I could not really test it or did not spend time making it very useful.

This is what you get for not planning out the solution and sitting down and just start coding.

You can use the current file or an external file.
You can use the different views shown in the pic of the context menu.
Double click on the image then select the insertion point to insert the block 
It must be the current drawing to produce images for blocks that do not have one. Once produced save drawing and the image will be saved with the drawing.
If you already have produced images for blocks you must close the palette and run the command "BasicBlockManagerShow" (just type "Basi" then Tab)
You can group them by Letters of the Alphabet
If you created drawings with a vertical like MEP you might want to filter out blocks that start with "Aec"


按此在新窗口打开图片
按此在新窗口打开图片
按此在新窗口打开图片

 
I added the projects for C# and VB

【HpadCadBasicBlockManager.zip】点击下载此文件
C# Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using System.Runtime.InteropServices;
namespace HpadCadBasicBlockManager
{
    public partial class BasicBlockMangerPalette : UserControl
    {
        private bool viewGroups = false;
        private static string dwgFileName = null;

        public BasicBlockMangerPalette()
        {
            InitializeComponent();
        }
        private void FillListView()
        {
            this.lvBlocks.Items.Clear();
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
                foreach (ObjectId objID in bt)
                {
                    BlockTableRecord btr = objID.GetObject(OpenMode.ForRead) as BlockTableRecord;
                    if (!(btr.IsLayout))
                    {
                        string blkName = btr.Name;
                        Bitmap blkThumbnail = btr.PreviewIcon as Bitmap;
                        if (blkThumbnail == null)
                        {
                            using (CreateBlockIcon cbi = new CreateBlockIcon(blkName))
                            {
                                blkThumbnail = btr.PreviewIcon as Bitmap;
                            }
                        }
                        if (blkThumbnail == null)
                        {                           
                            AddListViewItem(blkName);
                        }
                        else
                        {
                            AddBlockImageToList(blkName, blkThumbnail);
                            AddListViewItem(blkName, true);
                        }
                    }
                }// End foreach
                trx.Commit();
            }// End trx
        }// End FillListView

        private void FillListView(string fileName)
        {
            this.lvBlocks.Items.Clear();
            Database db = new Database(false, true) as Database;
            db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, true, null);
            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
                foreach (ObjectId objID in bt)
                {
                    BlockTableRecord btr = objID.GetObject(OpenMode.ForRead) as BlockTableRecord;
                    if (!(btr.IsLayout))
                    {
                        string blkName = btr.Name;
                        Bitmap blkThumbnail = btr.PreviewIcon as Bitmap;
                        if (blkThumbnail == null)
                        {
                            using (CreateBlockIcon cbi = new CreateBlockIcon(blkName))
                            {
                                blkThumbnail = btr.PreviewIcon as Bitmap;
                            }
                        }
                        if (blkThumbnail == null)
                        {
                            AddListViewItem(blkName);
                        }
                        else
                        {
                            AddBlockImageToList(blkName, blkThumbnail);
                            AddListViewItem(blkName, true);
                        }
                    }
                }// End foreach
                trx.Commit();
            }// End trx
        }// End FillListView

        private void AddBlockImageToList(string blkName,Bitmap blkThumbnail)
        {
            if (!(this.BlockLargeImage.Images.ContainsKey(blkName)))
            {
                this.BlockLargeImage.Images.Add(blkName, blkThumbnail);
                this.BlockSmallImage.Images.Add(blkName, blkThumbnail);
            }
        }// End AddBlockImageToList

        private void AddListViewItem(string blkName,bool blkThumbnailExists)
        {
            ListViewItem lvi = new ListViewItem(blkName, blkName) as ListViewItem;
            lvi.ToolTipText = blkName;
            this.lvBlocks.Items.Add(lvi);

        }// End AddListViewItem

        private void AddListViewItem(string blkName)
        {
            ListViewItem lvi = new ListViewItem(blkName) as ListViewItem;
            lvi.ToolTipText = blkName;
            this.lvBlocks.Items.Add(lvi);
        }// End AddListViewItem
        private void SetGroups(int column)
        {
            lvBlocks.Groups.Clear();
            List<string> groups = new List<string>();
            foreach (ListViewItem item in lvBlocks.Items)
                {
                   string groupName = item.Text.Substring(0, 1).ToUpper();
                    
                   if (groups.Contains(groupName))
                   {
                      item.Group = lvBlocks.Groups[groupName];
                   } 
                   else
                   {
                      groups.Add(groupName);
                      item.Group = lvBlocks.Groups.Add(groupName, groupName);
                  }
                }
        }
        #region "Context Menu Code"        
        private void BlocksContextMenu_Opening(object sender, CancelEventArgs e)
        {
            this.LargeIconToolStripMenuItem.Checked = false;
            this.DetailToolStripMenuItem.Checked = false;
            this.ListViewToolStripMenuItem.Checked = false;
            this.SmallIconToolStripMenuItem.Checked = false;
            this.TileViewToolStripMenuItem.Checked = false;
            this.useCurrentFileToolStripMenuItem.Checked = false;
            this.ChooseFileToolStripMenuItem.Checked = false;
            switch (this.lvBlocks.View)
            {
                case View.LargeIcon:
                    this.LargeIconToolStripMenuItem.Checked = true;
                    break;
                case View.Details:
                    this.DetailToolStripMenuItem.Checked = true;
                    break;
                case View.List:
                    this.ListViewToolStripMenuItem.Checked = true;
                    break;
                case View.SmallIcon:
                    this.SmallIconToolStripMenuItem.Checked = true;
                    break;
                case View.Tile:
                    this.TileViewToolStripMenuItem.Checked = true;
                    break;
            }
            ShowGroupsToolStripMenuItem.Checked = viewGroups;
            if (dwgFileName == null)
            {
                this.useCurrentFileToolStripMenuItem.Checked = true;
            }
            else
            {
                this.ChooseFileToolStripMenuItem.Checked = true;
            }
        }// End BlocksContextMenu_Opening

        private void LargeIconToolStripMenuItem_Click(Object sender, EventArgs e)
        {
            this.lvBlocks.View = View.LargeIcon;
        }
        private void SmallIconToolStripMenuItem_Click(Object sender, EventArgs e)
        {
            this.lvBlocks.View = View.SmallIcon;
        }
        private void DetailToolStripMenuItem_Click(Object sender, EventArgs e)
        {
            this.lvBlocks.View = View.Details;
        }
        private void ListViewToolStripMenuItem_Click(Object sender, EventArgs e)
        {
            this.lvBlocks.View = View.List;
        }

        private void TileViewToolStripMenuItem_Click(Object sender, EventArgs e)
        {
            this.lvBlocks.View = View.Tile;
        }
        private void ShowGroupsToolStripMenuItem_Click(Object sender, EventArgs e)
        {
            viewGroups = (!(viewGroups));
            lvBlocks.ShowGroups = viewGroups;
            ShowGroupsToolStripMenuItem.Checked = !ShowGroupsToolStripMenuItem.Checked;
            SetGroups(0);
        }
       
        private void ChooseFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OFD.Filter = "Block Files|*.dwg;*.dwt";
            OFD.Title = "Select Block File";
            OFD.Multiselect = false;
            OFD.ShowHelp = true;
            OFD.CheckPathExists = true;
            OFD.CheckFileExists = true;
            OFD.FilterIndex = 1;
            try
            {
                if (OFD.ShowDialog() == DialogResult.OK)
                {                   
                    dwgFileName = OFD.FileName;
                    FillListView(OFD.FileName);
                }
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }
        private void useCurrentFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dwgFileName = null;
            FillListView();
        }
        #endregion

        private void lvBlocks_DoubleClick(object sender, EventArgs e)
        {
            string blk = lvBlocks.FocusedItem.Text;
            if (dwgFileName == null)
            {
                InsertBlock(blk);
            }
            else
            {
                InsertBlock(blk, dwgFileName);
            }

        }
        private void InsertBlock(string blkName)
        {            
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            using (DocumentLock docloc = doc.LockDocument())
            using (Transaction trx = db.TransactionManager.StartTransaction())           
            {
                BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
                BlockTableRecord currBtr = db.CurrentSpaceId.GetObject(OpenMode.ForRead) as BlockTableRecord;
                if (!(bt.Has(blkName)))
                {
                    return;
                }
                ObjectId btrId = bt[blkName].GetObject(OpenMode.ForRead).ObjectId;
                PromptPointOptions ppo = new PromptPointOptions("/nSelect Insertion Point: ");
                PromptPointResult ppr = ed.GetPoint(ppo);
                if (ppr.Status == PromptStatus.OK)
                {
                    Point3d insertPnt = ppr.Value;
                    currBtr.UpgradeOpen();
                    BlockReference bref = new BlockReference(insertPnt, btrId);
                    currBtr.AppendEntity(bref);
                    trx.AddNewlyCreatedDBObject(bref, true);
                }
                trx.Commit();
            }
        }
        private void InsertBlock(string blkName, string fileName)
        {
            Database extDb = new Database(false, true) as Database;
            extDb.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, true, "");
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            using (DocumentLock docloc = doc.LockDocument())
            using (Transaction trx = db.TransactionManager.StartTransaction())
            using (Transaction extTrx = extDb.TransactionManager.StartTransaction())
            {                
                BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
              
                BlockTableRecord currBtr = db.CurrentSpaceId.GetObject(OpenMode.ForRead) as BlockTableRecord;
                if (!(bt.Has(blkName)))
                {
                    BlockTable extBt = extDb.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;                    
                    IdMapping map = new IdMapping();
                    ObjectIdCollection objIdColl = new ObjectIdCollection();
                    try
                    {
                        objIdColl.Add(extBt[blkName]);
                    }
                    catch
                    {
                        trx.Commit();
                        return;
                    }                    
                    db.WblockCloneObjects(objIdColl, bt.ObjectId, map, DuplicateRecordCloning.Replace, false);
                   
                }               
                      ObjectId btrId = bt[blkName].GetObject(OpenMode.ForRead).ObjectId;
                
                PromptPointOptions ppo = new PromptPointOptions("/nSelect Insertion Point: ");
                PromptPointResult ppr = ed.GetPoint(ppo);
                if (ppr.Status == PromptStatus.OK)
                {
                    Point3d insertPnt = ppr.Value;
                    currBtr.UpgradeOpen();
                    BlockReference bref = new BlockReference(insertPnt,btrId);
                    currBtr.AppendEntity(bref);
                    trx.AddNewlyCreatedDBObject(bref,true);
                }
                trx.Commit();
            }


        }
        private void BasicBlockMangerPalette_Load(object sender, EventArgs e)
        {
            this.lvBlocks.ContextMenuStrip = BlocksContextMenu;
            this.lvBlocks.LargeImageList = BlockLargeImage;
            this.lvBlocks.SmallImageList = BlockSmallImage;
            FillListView();
            this.lvBlocks.ShowGroups = viewGroups;
        }       
    }
    public class CreateBlockIcon : IDisposable
    {
        [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
        private static extern int acedCommand(int type1, string command, int type2, string blockName, int end);

        public CreateBlockIcon(string blkname)
        {
            acedCommand(5005, "BLOCKICON", 5005, blkname, 5000);
        }
        #region IDisposable Members
        public void Dispose()
        {
            
        }
        #endregion
    }
}


--------------------------------------------------------------------------------
【HpadCadBasicBlockManagerVB.zip】点击下载此文件

VB.Net Code: 
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.Interop.Common
Imports System.Runtime.InteropServices


Public Class BasicBlockMangerPalette
    Private viewGroups As Boolean = False
    Private Shared dwgFileName As String = Nothing
    Private Sub FillListView()
        Me.lvBlocks.Items.Clear()
        Dim doc As Document = DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Using trx As Transaction = db.TransactionManager.StartTransaction()
            Dim bt As BlockTable = TryCast(db.BlockTableId.GetObject(OpenMode.ForRead), BlockTable)
            For Each objID As ObjectId In bt
                Dim btr As BlockTableRecord = TryCast(objID.GetObject(OpenMode.ForRead), BlockTableRecord)
                If Not (btr.IsLayout) Then
                    Dim blkName As String = btr.Name
                    Dim blkThumbnail As Bitmap = TryCast(btr.PreviewIcon, Bitmap)
                    If blkThumbnail Is Nothing Then
                        Dim cbi As New CreateBlockIcon(blkName)
                        blkThumbnail = TryCast(btr.PreviewIcon, Bitmap)
                        cbi = Nothing
                    End If

                    If blkThumbnail Is Nothing Then
                        AddListViewItem(blkName)
                    Else
                        AddBlockImageToList(blkName, blkThumbnail)
                        AddListViewItem(blkName, True)
                    End If
                End If
            Next
            ' End foreach
            trx.Commit()
        End Using
        ' End trx
    End Sub
    ' End FillListView
    Private Sub FillListView(ByVal fileName As String)
        Me.lvBlocks.Items.Clear()
        Dim db As Database = TryCast(New Database(False, True), Database)
        db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, True, Nothing)
        Using trx As Transaction = db.TransactionManager.StartTransaction()
            Dim bt As BlockTable = (db.BlockTableId.GetObject(OpenMode.ForRead))
            For Each objID As ObjectId In bt
                Dim btr As BlockTableRecord = TryCast(objID.GetObject(OpenMode.ForRead), BlockTableRecord)
                If Not (btr.IsLayout) Then
                    Dim blkName As String = btr.Name
                    Dim blkThumbnail As Bitmap = TryCast(btr.PreviewIcon, Bitmap)
                    If blkThumbnail Is Nothing Then
                        Dim cbi As New CreateBlockIcon(blkName)
                        blkThumbnail = TryCast(btr.PreviewIcon, Bitmap)
                        cbi = Nothing
                    End If
                    If blkThumbnail Is Nothing Then
                        AddListViewItem(blkName)
                    Else
                        AddBlockImageToList(blkName, blkThumbnail)
                        AddListViewItem(blkName, True)
                    End If
                End If
            Next
            ' End foreach
            trx.Commit()
        End Using
        ' End trx
    End Sub
    ' End FillListView
    Private Sub AddBlockImageToList(ByVal blkName As String, ByVal blkThumbnail As Bitmap)
        If Not (Me.BlockLargeImage.Images.ContainsKey(blkName)) Then
            Me.BlockLargeImage.Images.Add(blkName, blkThumbnail)
            Me.BlockSmallImage.Images.Add(blkName, blkThumbnail)
        End If
    End Sub
    ' End AddBlockImageToList
    Private Sub AddListViewItem(ByVal blkName As String, ByVal blkThumbnailExists As Boolean)
        Dim lvi As ListViewItem = TryCast(New ListViewItem(blkName, blkName), ListViewItem)
        lvi.ToolTipText = blkName
        Me.lvBlocks.Items.Add(lvi)

    End Sub
    ' End AddListViewItem
    Private Sub AddListViewItem(ByVal blkName As String)
        Dim lvi As ListViewItem = TryCast(New ListViewItem(blkName), ListViewItem)
        lvi.ToolTipText = blkName
        Me.lvBlocks.Items.Add(lvi)
    End Sub
    ' End AddListViewItem
    Private Sub SetGroups(ByVal column As Integer)
        lvBlocks.Groups.Clear()
        Dim groups As New List(Of String)()
        For Each item As ListViewItem In lvBlocks.Items
            Dim groupName As String = item.Text.Substring(0, 1).ToUpper()

            If groups.Contains(groupName) Then
                item.Group = lvBlocks.Groups(groupName)
            Else
                groups.Add(groupName)
                item.Group = lvBlocks.Groups.Add(groupName, groupName)
            End If
        Next
    End Sub
#Region "Context Menu Code"
    Private Sub BlocksContextMenu_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles BlocksContextMenu.Opening
        Me.LargeIconToolStripMenuItem.Checked = False
        Me.DetailToolStripMenuItem.Checked = False
        Me.ListViewToolStripMenuItem.Checked = False
        Me.SmallIconToolStripMenuItem.Checked = False
        Me.TileViewToolStripMenuItem.Checked = False
        Me.useCurrentFileToolStripMenuItem.Checked = False
        Me.ChooseFileToolStripMenuItem.Checked = False
        Select Case Me.lvBlocks.View
            Case View.LargeIcon
                Me.LargeIconToolStripMenuItem.Checked = True
                Exit Select
            Case View.Details
                Me.DetailToolStripMenuItem.Checked = True
                Exit Select
            Case View.List
                Me.ListViewToolStripMenuItem.Checked = True
                Exit Select
            Case View.SmallIcon
                Me.SmallIconToolStripMenuItem.Checked = True
                Exit Select
            Case View.Tile
                Me.TileViewToolStripMenuItem.Checked = True
                Exit Select
        End Select
        ShowGroupsToolStripMenuItem.Checked = viewGroups
        If dwgFileName Is Nothing Then
            Me.useCurrentFileToolStripMenuItem.Checked = True
        Else
            Me.ChooseFileToolStripMenuItem.Checked = True
        End If
    End Sub
    ' End BlocksContextMenu_Opening
    Private Sub LargeIconToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LargeIconToolStripMenuItem.Click
        Me.lvBlocks.View = View.LargeIcon
    End Sub

    Private Sub SmallIconToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SmallIconToolStripMenuItem.Click
        Me.lvBlocks.View = View.SmallIcon
    End Sub

    Private Sub DetailToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DetailToolStripMenuItem.Click
        Me.lvBlocks.View = View.Details
    End Sub

    Private Sub ListViewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListViewToolStripMenuItem.Click
        Me.lvBlocks.View = View.List
    End Sub

    Private Sub TileViewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TileViewToolStripMenuItem.Click
        Me.lvBlocks.View = View.Tile
    End Sub

    Private Sub ShowGroupsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowGroupsToolStripMenuItem.Click
        viewGroups = (Not (viewGroups))
        lvBlocks.ShowGroups = viewGroups
        ShowGroupsToolStripMenuItem.Checked = Not ShowGroupsToolStripMenuItem.Checked
        SetGroups(0)
    End Sub

    Private Sub ChooseFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChooseFileToolStripMenuItem.Click
        OFD.Filter = "Block Files|*.dwg;*.dwt"
        OFD.Title = "Select Block File"
        OFD.Multiselect = False
        OFD.ShowHelp = True
        OFD.CheckPathExists = True
        OFD.CheckFileExists = True
        OFD.FilterIndex = 1
        Try
            If OFD.ShowDialog() = DialogResult.OK Then
                dwgFileName = OFD.FileName
                FillListView(OFD.FileName)
            End If
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub useCurrentFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles useCurrentFileToolStripMenuItem.Click
        dwgFileName = Nothing
        FillListView()
    End Sub
  
#End Region

    
    Private Sub InsertBlock(ByVal blkName As String)
        Dim doc As Document = DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        Using docloc As DocumentLock = doc.LockDocument()
            Using trx As Transaction = db.TransactionManager.StartTransaction()
                Dim bt As BlockTable = TryCast(db.BlockTableId.GetObject(OpenMode.ForRead), BlockTable)
                Dim currBtr As BlockTableRecord = TryCast(db.CurrentSpaceId.GetObject(OpenMode.ForRead), BlockTableRecord)
                If Not (bt.Has(blkName)) Then
                    Return
                End If
                Dim btrId As ObjectId = bt(blkName).GetObject(OpenMode.ForRead).ObjectId
                Dim ppo As New PromptPointOptions("/nSelect Insertion Point: ")
                Dim ppr As PromptPointResult = ed.GetPoint(ppo)
                If ppr.Status = PromptStatus.OK Then
                    Dim insertPnt As Point3d = ppr.Value
                    currBtr.UpgradeOpen()
                    Dim bref As New BlockReference(insertPnt, btrId)
                    currBtr.AppendEntity(bref)
                    trx.AddNewlyCreatedDBObject(bref, True)
                End If
                trx.Commit()
            End Using
        End Using
    End Sub
    Private Sub InsertBlock(ByVal blkName As String, ByVal fileName As String)
        Dim extDb As Database = TryCast(New Database(False, True), Database)
        extDb.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, True, "")
        Dim doc As Document = DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        Using docloc As DocumentLock = doc.LockDocument()
            Using trx As Transaction = db.TransactionManager.StartTransaction()
                Using extTrx As Transaction = extDb.TransactionManager.StartTransaction()
                    Dim bt As BlockTable = TryCast(db.BlockTableId.GetObject(OpenMode.ForRead), BlockTable)

                    Dim currBtr As BlockTableRecord = TryCast(db.CurrentSpaceId.GetObject(OpenMode.ForRead), BlockTableRecord)
                    If Not (bt.Has(blkName)) Then
                        Dim extBt As BlockTable = TryCast(extDb.BlockTableId.GetObject(OpenMode.ForRead), BlockTable)
                        Dim map As New IdMapping()
                        Dim objIdColl As New ObjectIdCollection()
                        Try
                            objIdColl.Add(extBt(blkName))
                        Catch
                            trx.Commit()
                            Return
                        End Try

                        db.WblockCloneObjects(objIdColl, bt.ObjectId, map, DuplicateRecordCloning.Replace, False)
                    End If
                    Dim btrId As ObjectId = bt(blkName).GetObject(OpenMode.ForRead).ObjectId

                    Dim ppo As New PromptPointOptions("/nSelect Insertion Point: ")
                    Dim ppr As PromptPointResult = ed.GetPoint(ppo)
                    If ppr.Status = PromptStatus.OK Then
                        Dim insertPnt As Point3d = ppr.Value
                        currBtr.UpgradeOpen()
                        Dim bref As New BlockReference(insertPnt, btrId)
                        currBtr.AppendEntity(bref)
                        trx.AddNewlyCreatedDBObject(bref, True)
                    End If
                    trx.Commit()
                End Using
            End Using
        End Using


    End Sub

    Private Sub lvBlocks_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvBlocks.DoubleClick
        Dim blk As String = lvBlocks.FocusedItem.Text
        If dwgFileName Is Nothing Then
            InsertBlock(blk)
        Else
            InsertBlock(blk, dwgFileName)
        End If
    End Sub
    Private Sub BasicBlockMangerPalette_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.lvBlocks.ContextMenuStrip = BlocksContextMenu
        Me.lvBlocks.LargeImageList = BlockLargeImage
        Me.lvBlocks.SmallImageList = BlockSmallImage
        FillListView()
        Me.lvBlocks.ShowGroups = viewGroups

    End Sub

   
  
    
End Class
Public Class CreateBlockIcon
    <DllImport("acad.exe", CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Unicode)> _
    Private Shared Function acedCommand(ByVal type1 As Integer, ByVal command As String, ByVal type2 As Integer, ByVal blockName As String, ByVal [end] As Integer) As Integer

    End Function
    Sub New(ByVal blkname As String)
        acedCommand(5005, "BLOCKICON", 5005, blkname, 5000)
    End Sub
End Class


[本日志由 tiancao1001 于 2018-10-25 05:48 PM 编辑]


暂时没有评论
发表评论 - 不要忘了输入验证码哦!
作者: 用户:  密码:   注册? 验证:  防止恶意留言请输入问题答案:1*9=?  
评论:

禁止表情
禁止UBB
禁止图片
识别链接
识别关键字

字体样式 文字大小 文字颜色
插入粗体文本 插入斜体文本 插入下划线
左对齐 居中对齐 右对齐
插入超级链接 插入邮件地址 插入图像
插入 Flash 插入代码 插入引用
插入列表 插入音频文件 插入视频文件
插入缩进符合
点击下载按钮 下标 上标
水平线 简介分割标记
表  情
 
Tiancao Blog All Rights Reserved 田草博客 版权所有
Copyright ©