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. 基于SpringCloud的Microservices架构实战案例

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  2. 【朝花夕拾】Android自定义View篇之(十)TouchSlop及VelocityTracker

    前言 在Android事件中,有几个比较基本的概念和知识点需要掌握.比如,表示最小移动阈值的TouchSlop,追踪事件速度的VelocityTracker,用于检测手势的GestureDetecto ...

  3. codeblocks出现'to_string' was not declared in this scope 的问题,用g++11编译环境

    在将数字转化为字符串时使用to_string()竟然出现了'to_string' was not declared in this scope,我头文件用的万能头文件肯定没问题,而这个函数在其他的CB ...

  4. Hamcrest的简单应用

    import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import java.util.ArrayList; ...

  5. CNN神经网络之卷积操作

    在看这两个函数之前,我们需要先了解一维卷积(conv1d)和二维卷积(conv2d),二维卷积是将一个特征图在width和height两个方向进行滑动窗口操作,对应位置进行相乘求和:而一维卷积则只是在 ...

  6. springboot项目问题记录one

    上面三个方法描述如下: 首先有个业务,我是需要调取第三方一个sdk,然后sdk里面封装的kafka,也就是说,需要用sdk内置的连接kafka去消费消息,然后又有一个类需要实现Message,此Mes ...

  7. Excel催化剂开源第2波-自动检测Excel的位数选择对应位数的xll文件安装

    Excel插件的部署问题难倒了不了的用户,特别是VSTO的部署,用ExcelDna开发的xll文件部署方便,不挑用户机器环境,是其开发Excel插件的一大优势. 其开发出来的xll文件,最终还是需要考 ...

  8. django中ORM的model对象和querryset 简单解析

    欢迎大家查看我的博客,我会不定时的用大白话发一些看了就能懂的文章,大家多多支持!如您对此文章内容有独特见解,欢迎与笔者练习一起探讨学习!原创文创!转载请注明出处! ORM是干嘛的? 介绍orm之前我应 ...

  9. eclipse(javaee windows)

    百度云:链接:http://pan.baidu.com/s/1eSoO4s6   密码:54am 官方下载网址:http://www.eclipse.org/downloads/eclipse-pac ...

  10. 《C# 语言学习笔记》——C# 简介

    1 什么是.NET Framework .NET Framework 是Microsoft为开发应用程序而创建的一个富有革命性的新平台. 1.1 .NET Framework 的内容 .NET Fra ...