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. Java 源码学习系列(三)——Integer

    Integer 类在对象中包装了一个基本类型 int 的值.Integer 类型的对象包含一个 int 类型的字段. 此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还 ...

  2. Senparc.Weixin.MP SDK 微信公众平台开发教程(二十一):在小程序中使用 WebSocket (.NET Core)

    本文将介绍如何在 .NET Core 环境下,借助 SignalR 在小程序内使用 WebSocket.关于 WebSocket 和 SignalR 的基础理论知识不在这里展开,已经有足够的参考资料, ...

  3. PostgreSQL 窗口函数 ( Window Functions ) 如何使用?

    一.为什么要有窗口函数 我们直接用例子来说明,这里有一张学生考试成绩表testScore: 现在有个需求,需要查询的时候多出一列subject_avg_score,为此科目所有人的平均成绩,好跟每个人 ...

  4. PHP---微信JS-SDK获取access_token/jsapi_ticket/signature权限签名算法,php/thinkphp实现微信分享自定义文字和图片

    PHP---微信JS-SDK获取access_token/jsapi_ticket/signature权限签名算法, php/thinkphp实现微信分享自定义文字和图片. 一.先看微信JS-SDK文 ...

  5. a=re.findall('b',c)报错提示:TypeError:expected string or buffer

    目的:想通过findall选取某个unicode编码的字符串列表(列表里面有元组) 问题:报错[TypeError:expected string or buffer] 现在测试下: 定义一个有元组的 ...

  6. 前端插件之Bootstrap Dual Listbox使用

    工欲善其事,必先利其器 对于很多非专业前端开发来说写页面是非常痛苦的,借助框架或插件往往能够达到事半功倍的效果,本系列文章会介绍我在运维系统开发过程中用到的那些顺手的前端插件,如果你是想写XX管理系统 ...

  7. Node.js实现简易的获取access_token

    还是老样子,在自学node.js的道路上走得坑坑洼洼,按住了躁动的自己,调整好心情 ,ready........Go....! 首先在项目里新建config.json,其中 appid 与 appsc ...

  8. docker实战(一)之Tomcat的安装

           docker号称分分钟就可以将环境构建完成,这话一点也不假,因为docker在使用软件时只需要从官方 仓库中拉取对应的镜像就行了.docker的使用前需要了解两个名词--镜像和容器.这两 ...

  9. 一个项目中:只能存在一个 WebMvcConfigurationSupport (静态文件失效之坑)

    一个项目中:只能存在一个 WebMvcConfigurationSupport 在一个项目中WebMvcConfigurationSupport只能存在一个,多个的时候,只有一个会生效. 静态文件访问 ...

  10. 【部分转载】:【lower_bound、upperbound讲解、二分查找、最长上升子序列(LIS)、最长下降子序列模版】

    二分 lower_bound lower_bound()在一个区间内进行二分查找,返回第一个大于等于目标值的位置(地址) upper_bound upper_bound()与lower_bound() ...