Author Topic: CLR equivalents ?  (Read 8712 times)

0 Members and 1 Guest are viewing this topic.

Spike Wilbury

  • Guest
Re: CLR equivalents ?
« Reply #15 on: September 19, 2008, 12:28:39 AM »
Here is the solution:

ARX function
Code: [Select]
extern "C" __declspec(dllexport)
resbuf *GbPeSSPoly(AcDbObjectIdArray &ids,
  const TCHAR *LayerName,
  const TCHAR *LayerNameOut,
  double dtol,
  short poltip,
  double areamin,
  short inpol,
  short outpol,
  short dirpolhor)
{
resbuf* head=NULL;
resbuf* next=NULL;
// layers exists?
AcDbLayerTable *LayerTable=NULL;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(LayerTable,AcDb::kForRead);
if(!LayerTable->has(LayerName))
LayerName=_T("0"); // default
if(!LayerTable->has(LayerNameOut))
LayerNameOut=_T("0"); // default
LayerTable->close();
ads_name sspol;
if (!ids.isEmpty())
gbpoly(ids,LayerName,LayerNameOut,dtol,poltip,areamin,inpol,outpol,dirpolhor,sspol);
// now verify if the selection set has some generated polylines
long len=0;
if ((acedSSLength(sspol,&len) != RTNORM) || (len==0))
{
acedSSFree(sspol);
return head;
}
// fill the output array of object ids
for (long i=0; i<len; i++)
{
AcDbObjectId objId;
ads_name ename;
acedSSName(sspol,i,ename);
if (acdbGetObjectId(objId,ename) != Acad::eOk) return head;
if(!head)
{
head=acutNewRb(RTLONG);
next=head;
}
else
{
next->rbnext=acutNewRb(RTLONG);
next=next->rbnext;
}
next->resval.rlong=objId.asOldId();
}
acedSSFree(sspol);
return head;
acutRelRb(head);
}

And from C#
Code: [Select]
[DllImport("GbPolyEngine17.arx", CharSet = CharSet.Unicode, EntryPoint = "GbPeSSPoly")]
static extern IntPtr pGbPeSSPoly(IntPtr ptrids, string layerInternal, string layerExternal,
    double tol, short poltype, double minarea, short inpol, short outpol, short poldir);

public static ObjectIdCollection GbPeSSPoly(ObjectIdCollection ids, string layerInternal, string layerExternal,
    double tol, short poltype, double minarea, short inpol, short outpol, short poldir)
{
    ObjectIdCollection oic = new ObjectIdCollection();
    IntPtr ip = IntPtr.Zero;
    ip = pGbPeSSPoly(ids.UnmanagedObject, layerInternal, layerExternal, tol, poltype, minarea, inpol, outpol, poldir);
    if (ip != IntPtr.Zero)
    {
        try
        {
            ResultBuffer rb = DisposableWrapper.Create(typeof(ResultBuffer), ip, true) as ResultBuffer;
            foreach (TypedValue tv in rb)
            {
                ObjectId id = new ObjectId((int)tv.Value);
                oic.Add(id);
            }
            rb.Dispose();
        }
        catch { return null; }
    }
    return oic;
}

[CommandMethod("SsPoly")]
static public void GbPolySsPoly()
{
    Document doc = AcadApp.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    Database db = doc.Database;
    PromptSelectionOptions prOpts = new PromptSelectionOptions();
    prOpts.MessageForAdding = "\nSelect curve objects: ";
    TypedValue[] filter = new TypedValue[1];
    Object obj = "LINE,ARC,LWPOLYLINE,POLYLINE,CIRCLE";
    filter[0] = new TypedValue(0, obj);
    SelectionFilter ssFilter = new SelectionFilter(filter);
    PromptSelectionResult res = ed.GetSelection(prOpts, ssFilter);
    if (res.Status != PromptStatus.OK) return;
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        string layerInternal = "TESTINT";
        string layerExternal = "TESTEXT";
        double tol = 0.5;
        short poltype = 1;
        double minarea = 20.0;
        short inpol = 1;
        short outpol = 1;
        short poldir = 1;
        ObjectIdCollection inOids = new ObjectIdCollection();
        ObjectIdCollection outIds = new ObjectIdCollection();
        foreach (ObjectId id in res.Value.GetObjectIds())
            inOids.Add(id);
        try
        {
            outIds = GbPeSSPoly(inOids, layerInternal, layerExternal, tol, poltype, minarea, inpol, outpol, poldir);
            if (outIds.Count > 0)
                ed.WriteMessage("\nNumber[{0}]", outIds.Count.ToString());
        }
        catch (System.Exception ex)
        {
            ed.WriteMessage("Error: " + ex.Message);
        }
        tr.Commit();
    }
}

:)

HTH

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8718
  • AKA Daniel
Re: CLR equivalents ?
« Reply #16 on: September 19, 2008, 10:49:24 PM »
Hi Luis,

I had a chance to play around with this a little. Here is an example of how to do AcDbObjectIdArray <--> ObjectIdCollection

Code: [Select]
extern "C" __declspec(dllexport)
void pTest(AcDbObjectIdArray &ids)
{
  Acad::ErrorStatus es;
  int counter = 0;
  AcDbBlockTableRecord *pTableRecord;
  AcDbDatabase *pDatabase =
    acdbHostApplicationServices()->workingDatabase();
  AcDbBlockTablePointer
    pBlockTable(pDatabase,AcDb::kForRead);
  AcDbBlockTableIterator *pBlockTableIterator;
  pBlockTable->newIterator(pBlockTableIterator);
  for (pBlockTableIterator->start();
       !pBlockTableIterator->done();
        pBlockTableIterator->step())
  {
    es = pBlockTableIterator->getRecord(pTableRecord, AcDb::kForRead,Adesk::kFalse);
    if (es == Acad::eOk &&  pTableRecord->hasAttributeDefinitions() == Adesk::kTrue)
    {
      AcDbObjectIdArray blockReferenceIds;
      pTableRecord->getBlockReferenceIds(blockReferenceIds);
      ids.append(blockReferenceIds);
      pTableRecord->close();
    }
  }
  delete pBlockTableIterator;
}


Code: [Select]
public class Commands
    {
        [DllImport("LuisArx.arx",CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr pTest(IntPtr ptrIds);

        [CommandMethod("doit")]
        static public void test()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            try
            {
                ObjectIdCollection ids = new ObjectIdCollection();
                pTest(ids.UnmanagedObject);
                ed.WriteMessage(ids.Count.ToString());
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message);
            }
        }
    }

Spike Wilbury

  • Guest
Re: CLR equivalents ?
« Reply #17 on: September 20, 2008, 12:07:49 AM »
Hi Daniel,

Don't tell me....  :oops: --- well, what can I say....

I'll be doing the update to my project as your code sample.


Thank you.
Luis.