Wrapper to C#, Success!(CAD2008 + VS2005)
step1: Create a new ARX project
step2: Add a class: MyClass
step3: In MyClass.h, Add code:
#include "tchar.h"
step4: In MyClass.cpp, Add code:
extern "C" __declspec(dllexport) AcDb3dSolid* GetSubentIndexs()
{
AcDbObjectId entId = NULL;
AcArray<AcDbSubentId*> subIdArr;
AcEdSolidSubentitySelector subSel;
AcDb3dSolid *pSolid3d = NULL;
if (subSel.selectFaces(entId, subIdArr) == Acad::eOk)
{
acdbOpenObject(pSolid3d, entId, AcDb::kForWrite);
TCHAR subIdIndexStrs[1000];
TCHAR indexStr[10];
int index = 0;
for (int i = 0; i < subIdArr.length(); i++)
{
index = (int)(subIdArr[i]->index());
_itow(index, indexStr, 10);
if (i == 0)
{
wcscpy(subIdIndexStrs, indexStr);
}
else
{
wcscat(subIdIndexStrs, _T("-"));
wcscat(subIdIndexStrs, indexStr);
}
}
struct resbuf rb;
rb.restype = RTSTR;
rb.resval.rstring = subIdIndexStrs;
acedSetVar(_T("users1"), &rb);
return pSolid3d;
}
return NULL;
}
step5: Compile, Generate *.arx file.
step6: copy the file to AutoCAD Support Directory
strp7: Create a new C# project, Add code
using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace Cs
{
public class Class1
{
[DllImport("yourARX.arx", CallingConvention = CallingConvention.Cdecl, EntryPoint = "GetSubentIndexs")]
public static extern IntPtr GetSubentIndexs();
[CommandMethod("test")]
public void MyTest()
{
Color myColor = Color.FromColorIndex(ColorMethod.ByLayer, 1);
IntPtr rb = GetSubentIndexs();
if (rb != IntPtr.Zero)
{
Solid3d solid3dEnt = DisposableWrapper.Create(typeof(Solid3d), rb, true) as Solid3d;
if (solid3dEnt != null)
{
string users1Value = (string)Application.GetSystemVariable("users1");
String[] strs = users1Value.Split(new char[] { '-' });
for (int i = 0; i < strs.Length; i++)
{
SubentityId subId = new SubentityId(SubentityType.Face, Convert.ToInt16(strs[i]));
solid3dEnt.SetSubentityColor(subId, myColor);
}
solid3dEnt.Close();
}
}
}
}
}