ObjectArx编程论坛忽悠悠原创
1,首先在解决方案中建立一个C#项目,用来注册命令
2,然后建立一个C++类库,用来实现托管代码与Arx非托管代码的衔接.具体的设置方法如下:
在项目属性中添加引用acdbmgd和acmgd,并且添加对应Arx版本的lib和inc目录
然后在Stdafx.h中添加如下代码
#include <Windows.h>
#include "arxHeaders.h"
#include "mgdinterop.h"
添加一个新类DrawLine,用来绘制一条直线,代码如下
- #pragma once
- using namespace System;
- using namespace System::Diagnostics;
- using namespace System::Collections;
- using namespace System::Text;
- using namespace System::Runtime::InteropServices;
- using namespace Autodesk::AutoCAD::ApplicationServices;
- using namespace Autodesk::AutoCAD::Runtime;
- using namespace Autodesk::AutoCAD::DatabaseServices;
- using namespace Autodesk::AutoCAD::EditorInput;
- using namespace Autodesk::AutoCAD::Geometry;
- namespace Tools {
- public ref class DrawLine
- {
- public:
- static ObjectId Draw(Point3d ptS, Point3d ptE)
- {
- AcGePoint3d gePtS, gePtE;
- gePtS = GETPOINT3D(ptS);
- gePtE = GETPOINT3D(ptE);
- AcDbLine *pLine = new AcDbLine(gePtS, gePtE);
- AcDbObjectId objId = AcDbObjectId::kNull;
- if (AppendEntity(pLine, objId))
- {
- pLine->close();
- return ToObjectId(objId);
- }
- else
- {
- return ObjectId::Null;
- }
- }
- private:
- //将实体加入模型空间
- static bool AppendEntity(AcDbEntity *pEnt,AcDbObjectId& objId)
- {
- bool bValue=true;
- AcDbBlockTable *pBlockTable;
- acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable, AcDb::kForRead);
- AcDbBlockTableRecord *pBlockTableRecord;
- pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);
- pBlockTable->close();
- if ( Acad::eOk != pBlockTableRecord->appendAcDbEntity(objId, pEnt) ) bValue=false;
- pBlockTableRecord->close();
- return bValue;
- }
- };
复制代码
3,设置解决方案属性中的"项目依赖项",将C#项目的依赖项设置为C++项目
4,给C#项目添加引用,在项目选项卡里面选择C++项目,然后添加如下代码
- using System;
- using System.Text;
- using System.Collections;
- using System.IO;
- using System.Windows.Forms;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Tools;
- namespace Test
- {
- public class Command
- {
- [CommandMethod("Test")]
- public void Test()
- {
- DrawLine.Draw(new Point3d(0, 0, 0), new Point3d(1, 1, 1));
- }
- }
- }
复制代码
|