https://blog.csdn.net/dengyiyu/article/details/2201175

本文主要给大家介绍一下SmartSoft中用C#.Net实现AutoCAD块属性提取的方法,并给出实例代码与大家共享。此类实现AutoCAD块属性提取功能,在VS.Net2003(2005)+AutoCAD2004(2007)下调试通过。

using System;
  using AutoCAD=Autodesk.AutoCAD.Interop;
  using System.Runtime.InteropServices ;
  using dbx = Autodesk.AutoCAD.Interop.Common;
  namespace SmartSoft.ACAD
  {
  ///
  /// 读取AutoCAD属性信息
  ///
  public class AutoCADConnector:IDisposable
  {
  private AutoCAD.AcadApplication _Application;
  private bool _Initialized;
  private bool _Disposed;
  #region 类初始化及析构操作
  ///
  /// 类初始化,试图获取一个正在运行的AutoCAD实例,
  /// 如果没有则新起动一个实例。
  ///
  public AutoCADConnector()
  {
  try
  {
  //取得一个正在运行的AUTOCAD实例
  this._Application = (AutoCAD.AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17");
  }//end of try
  catch
  {
  try
  {
  //建立一个新的AUTOCAD实例,并标识已经建立成功。
  _Application = new AutoCAD.AcadApplicationClass();
  _Initialized=true;
  }
  catch
  {
  throw new Exception ("无法起动AutoCAD应用程序,确认已经安装");
  }
  }//end of catch
  }//end of AutoCADConnector
  ~AutoCADConnector()
  {
  Dispose(false);
  }
  public void Dispose()
  {
  Dispose(true);
  GC.SuppressFinalize(this);
  }
  protected virtual void Dispose(bool disposing)
  {
  if (!this._Disposed && this._Initialized )
  {
  //如果建立了AUTOCAD的实列,调用QUIT方法以避免内存漏洞
  this._Application.ActiveDocument.Close (false,"");
  this._Application.Quit();
  this._Disposed=true;
  }
  }
  #endregion
  #region 公共用户接口属性
  ///
  /// 取得当前类所获得的AUTOCAD实例
  ///
  public AutoCAD.AcadApplication Application
  {
  get
  {
  return _Application;
  }
  }//end of Application
  #endregion
  #region 公共用户接口方法
  ///
  /// 根据给定的文件名以AxDbDocument类型返回该文档
  ///
  public dbx.AxDbDocument GetThisDrawing(string FileName,string PassWord)
  {
  ACAD.AutoCADConnector Connector=new AutoCADConnector();
  //这是AutoCAD2004的Programe ID
  string programeID ="ObjectDBX.AxDbDocument.17";
  AutoCAD.AcadApplication AcadApp = Connector.Application;
  dbx.AxDbDocument dbxDoc;
  dbxDoc=(dbx.AxDbDocument)AcadApp.GetInterfaceObject(programeID);
  try
  {
  if (System.IO.File.Exists (FileName)==false) throw new Exception ("文件不存在。");
  dbxDoc.Open(FileName,PassWord);
  }// end of try
  catch (Exception e)
  {
  System.Windows.Forms.MessageBox.Show(e.Message );
  dbxDoc=null;
  }
  return dbxDoc;
  }//end of function GetThisDrawing
  ///
  /// 根据当前文档和块名取得当前块的引用
  ///
  public dbx.AcadBlockReference GetBlockReference(dbx.AxDbDocument thisDrawing,string blkName)
  {
  dbx.AcadBlockReference blkRef=null;
  bool found = false;
  try
  {
  foreach (dbx.AcadEntity entity in thisDrawing.ModelSpace )
  {
  if (entity.EntityName=="AcDbBlockReference")
  {
  blkRef=(dbx.AcadBlockReference)entity;
  //System.Windows.Forms.MessageBox.Show(blkRef.Name);
  if (blkRef.Name.ToLower() ==blkName.ToLower() )
  {
  found = true;
  break;
  }
  }//end of entity.EntityName=="AcDbBlockReference"
  }// end of foreach thisDrawing.ModelSpace
  }//end of try
  catch (Exception e)
  {
  System.Windows.Forms.MessageBox.Show ("图形中有未知的错误,格式不正确或图形数据库需要修愎。系统错误提示:" + e.Message ,"信息",System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
  thisDrawing=null;
  }//end of catch
  if (!found) blkRef = null;
  return blkRef;
  }//end of function GetBlockReference
  ///
  /// 根据给定的块引用(dbx.AcadBlockReference)和属性名返回属性值
  ///
  public object GetValueByAttributeName(dbx.AcadBlockReference blkRef,string AttributeName)
  {
  object[] Atts=(object[])blkRef.GetAttributes();
  object attValue=null;
  for (int i=;i
  {
  dbx.AcadAttributeReference attRef;
  attRef=(dbx.AcadAttributeReference)Atts[i];
  if (attRef.TagString==AttributeName)
  {
  attValue= attRef.TextString ;
  break;
  }
  }//end of for i
  return attValue;
  }// end of function
  #endregion
  }//end of class CAutoCADConnector
  }//end of namespace AutoCAD

C#.Net实现AutoCAD块属性提取的更多相关文章

  1. AutoCAD开发1---获取块属性

    Private Sub CommandButton1_Click() Dim pEntity As AcadObject Dim pBlock As AcadBlockReference Dim pP ...

  2. arcgis操作笔记-根据属性提取某区域要素

    1. 提取

  3. 定位absolute使内联支持宽高(块属性变为内联,内容默认撑开)margin auto 失效

    relative   没脱离文档流 absdute 完全脱离文档流 margin :auto 失效 相对整个文档偏离 相对父级定位 fixed 脱离文档流 与绝对定位特性一致 3.P标快不能包块级标签 ...

  4. Oracle EBS FORM 设置块属性

    declare blk_id BLOCK; begin blk_id := Find_block('ADRP_HEADER'); Set_block_property(blk_id,insert_al ...

  5. CAD增强属性块的还原(转)

    来自:http://blog.3snews.net/space.php?uid=13924959&do=blog&id=70174 作者:毛毛虫 Demo下载:CAD增强属性块的还原 ...

  6. 软件-绘图-AutoCAD:百科

    ylbtech-软件-绘图-AutoCAD:百科 AutoCAD(Autodesk Computer Aided Design)是Autodesk(欧特克)公司首次于1982年开发的自动计算机辅助设计 ...

  7. 梦想CAD控件图块COM接口知识点

    梦想CAD控件图块COM接口知识点 图块是将多个实体组合成一个整体,并给这个整体命名保存,在以后的图形编辑中图块就被视为一个实体.一个图块包括可见的实体如线.圆.圆弧以及可见或不可见的属性数据.图块的 ...

  8. Python【BeautifulSoup解析和提取网页数据】

    [解析数据] 使用浏览器上网,浏览器会把服务器返回来的HTML源代码翻译为我们能看懂的样子 在爬虫中,也要使用能读懂html的工具,才能提取到想要的数据 [提取数据]是指把我们需要的数据从众多数据中挑 ...

  9. kaggle数据挖掘竞赛初步--Titanic<派生属性&维归约>

    完整代码: https://github.com/cindycindyhi/kaggle-Titanic 特征工程系列: Titanic系列之原始数据分析和数据处理 Titanic系列之数据变换 Ti ...

随机推荐

  1. GreenPlum完全安装_GP5.11.3完整安装

    1 概述 1.1 背景 1.2 目标 1.3 使用对象 2 配置系统信息 2.1 配置系统信息,做安装Greenplum的准备工作 Greenplum 数据库版本5.11.3 2.1.1 Greenp ...

  2. 微信小程序开发--组件(5)

    一.editor 富文本编辑器,可以对图片.文字进行编辑. 编辑器导出内容支持带标签的 html和纯文本的 text,编辑器内部采用 delta 格式进行存储. 通过setContents接口设置内容 ...

  3. 【题解】搬书-C++

    搬书 Description 陈老师桌上的书有三堆,每一堆都有厚厚的一叠,你想逗一下陈老师,于是你设计一个最累的方式给他,让他把书 拿下来给同学们.若告诉你这三堆分别有i,j,k本书,以及每堆从下到上 ...

  4. Windows Presentation Foundation (WPF) 项目中不支持xxx的解决

    一般Windows Presentation Foundation (WPF) 项目中不支持xxx都是由于没引用相应的程序集导致,比如Windows Presentation Foundation ( ...

  5. 算法学习笔记,几个简单的Demo

    算法初学的一些心得 前言:现在工作也快一年多了,有时间下班回家会学学算法,陆陆续续也接触了一些 貌似我知道的就冒泡排序其他的都不是很了解 最近买了一本书,边学边记录吧! 一些常用的方法 暴力破解 下面 ...

  6. 个人永久性免费-Excel催化剂功能第43波-文本处理类函数增强

    Excel的函数有400多个,真正常用的50多个,而常有的文本处理类函数也不多,不是因为文本类处理简单,而是Excel真的有点挤牙膏式的每个版本更新那么几个小函数,普通用户等得急切,但实际上这些小函数 ...

  7. 架构师小跟班:SSL证书免费申请及部署,解决页面样式错乱问题完整攻略

    申请证书 1.登录阿里云控制台,产品与服务,选择SSL证书 2.进入SSL证书页面,点击“购买证书”,选择免费1年的证书类型,点击“立即购买” 3.返回SSL证书页面,可以看到证书列表里多了一条记录 ...

  8. Sublime Text 格式化代码

    1.添加快捷键 其实在sublime中已经自建了格式化按钮: Edit -> Line -> Reindent 只是sublime并没有给他赋予快捷键,所以只需加上快捷键即可 Prefer ...

  9. 【Java中级】(一)面向对象的特性与八种基本类型

    1.1.Java 基本数据类型: Java提供了8种基本类型.六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型. byte.short.int.long.float.double. ...

  10. git pull 出现non-fast-forward的错误

    1.git pull origin daily_liu_0909:liu_0909 出现non-fast-forward的错误,证明您的本地库跟远程库的提交记录不一致,即 你的本地库版本需要更新2.g ...