Author Topic: Something about Xref functions...  (Read 9963 times)

0 Members and 1 Guest are viewing this topic.

LE

  • Guest
Something about Xref functions...
« on: December 19, 2006, 12:15:03 PM »
I kept learning as I can all about this C# stuff, not much, but I am, at least trying.... he he

Here is a tryout to convert an attach xref to overlay - without luck of course, it is something I never tried on objectARX by the way... and also, there is no single sample anywhere (I think).

Code: [Select]
[CommandMethod("ATOVERLAY")]
public void attachTooverlay()
{
    Document doc = acadApp.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    Database db = doc.Database;
    //PromptEntityOptions prOpt = new PromptEntityOptions("\nSelect Xref: ");
    //prOpt.SetRejectMessage("\nNot an Xref!");
    //prOpt.AddAllowedClass(typeof(BlockReference), true);
    //PromptEntityResult rs = ed.GetEntity(prOpt);
    PromptEntityResult res = ed.GetEntity("\nSelect xref: ");
    if (res.Status != PromptStatus.OK) return;
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        BlockReference blkRef = tr.GetObject(res.ObjectId, OpenMode.ForWrite, false) as BlockReference;
        if (blkRef != null)
        {
            BlockTableRecord blkRec = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForWrite);
            if (blkRec.IsFromOverlayReference == false)
            {
                //db.DetachXref(blkRec.ObjectId);
                //db.ResolveXrefs(true, true);

                string fileName, blockName;
                fileName = blkRec.PathName;
                blockName = blkRec.Name;
                ed.WriteMessage("\nPath: " + fileName);
                ed.WriteMessage("\nBlockname: " + blockName);
                ObjectId xrefId = db.OverlayXref(fileName, blockName);
                if (xrefId.IsValid == true)
                    ed.WriteMessage("\nIs a valid object id...");
                else
                    ed.WriteMessage("\nNo valid object id...");

                db.ResolveXrefs(true, true);
            }
        }
        tr.Commit();
    }
}

Since the OverlayXref() returns an ID.... what I should do with it?  or to make it work.... I tried first to detach the xref (it does that) but when I try to tun the overlay part... nothing.... nada.... niep.... nil

Thanks!

LE

  • Guest
Re: Something about Xref functions...
« Reply #1 on: December 19, 2006, 04:04:24 PM »
Here is my try using ARX and is working, still is a function in progress - mostly the undefined block message requires to get fix.... Will kept trying with the C# code...

Code: [Select]
static void changeToOverlay (void)
{
ads_name ename;
ads_point pt;
AcDbObjectId xrefBlkId;
AcDbDatabase* pDb = NULL;
pDb = acdbHostApplicationServices()->workingDatabase();
if (acedEntSel(_T("\nSelect an Xref: "), ename, pt) != RTNORM) return;
if (acdbGetObjectId (xrefBlkId, ename) != Acad::eOk) return;
AcDbObjectPointer<AcDbBlockReference> pRef (xrefBlkId, AcDb::kForWrite);
if (pRef.openStatus() != Acad::eOk) return;
//acutPrintf(_T("\nBlockReference open..."));
AcDbBlockTableRecordPointer pBtr (pRef->blockTableRecord(), AcDb::kForWrite);
if (pBtr.openStatus() != Acad::eOk) return;
//acutPrintf(_T("\nBlockTableRecord open..."));

ACHAR *pFilename;
if (pBtr->pathName(pFilename) != Acad::eOk) return;
acutPrintf(_T("\nPath: %s"), pFilename);

ACHAR *pBlockName;
if (pBtr->getName (pBlockName) != Acad::eOk) return;
acutPrintf(_T("\nBlockname: %s"), pBlockName);

pBtr->close(); // no needed anymore

if (acedXrefDetach(pBlockName) != Acad::eOk) return;
acutPrintf(_T("\nDetaching block..."));

pRef->recordGraphicsModified();

if (acedXrefOverlay(pFilename, pBlockName) != Acad::eOk) return;
acutPrintf(_T("\nChanging to overlay..."));

pRef->recordGraphicsModified();
}

T.Willey

  • Needs a day job
  • Posts: 5241
Re: Something about Xref functions...
« Reply #2 on: December 19, 2006, 04:49:51 PM »
So the only way it to detach it, and reattach it as an overlay in both languages?  I was hoping that it would be some function that would just switch it.  I didn't see a way in C# when I looked, but was hoping you would find one Luis.  Thanks for keeping us posted.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: Something about Xref functions...
« Reply #3 on: December 19, 2006, 05:26:21 PM »
So the only way it to detach it, and reattach it as an overlay in both languages?  I was hoping that it would be some function that would just switch it.  I didn't see a way in C# when I looked, but was hoping you would find one Luis.  Thanks for keeping us posted.

That's how I was able to make it work, but just the ARX code... the C# nope...

Alexander Rivilis

  • Bull Frog
  • Posts: 212
  • Programmer from Kyiv (Ukraine)

LE

  • Guest
Re: Something about Xref functions...
« Reply #5 on: December 21, 2006, 01:27:27 PM »
P/Invoke? http://discussion.autodesk.com/thread.jspa?messageID=5206856

Thank you Alexander;

I saw the code, thanks.

If I use my attachTooverlay() function, it is returning and ID of a new TableRecord no?.... what are we supposed to do with that?
Now, if I first detach the xref, and then tried to run the overlay method, it does not work.... why?

LE

  • Guest
Re: Something about Xref functions...
« Reply #6 on: December 21, 2006, 01:42:02 PM »
This is the latest code I have for that function....

Code: [Select]
        [CommandMethod("OVERLAY")]
        public void attachTooverlay()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            PromptEntityResult res = ed.GetEntity("\nSelect xref: ");
            if (res.Status != PromptStatus.OK) return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                ObjectId objId = res.ObjectId;
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                DBObject dbObj = tr.GetObject(objId, OpenMode.ForRead);
                BlockReference blkRef = dbObj as BlockReference;
                if (blkRef != null)
                {
                    BlockTableRecord blkDef = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForWrite);
                    if (blkDef.IsFromOverlayReference == false)
                    {
                        string fileName, blockName;
                        fileName = blkDef.PathName;
                        blockName = blkDef.Name;
                        ed.WriteMessage("\nPath: " + fileName);
                        ed.WriteMessage("\nBlockname: " + blockName);
                        doc.LockDocument();
                        ObjectId xrefId = db.OverlayXref(fileName, blockName);
                    }
                }

                tr.Commit();
            }
        }

LE

  • Guest
Re: Something about Xref functions...
« Reply #7 on: December 21, 2006, 01:48:29 PM »
And I tried also, to implement it with ObjectARX.... but was not possible :-(

OverlayXref() is a wrap of acdbOverlayXref()

Note: the below code, does not work....
Code: [Select]
static void changeToOverlay1 (void)
{
Acad::ErrorStatus es = Acad::eOk;
AcDbDatabase* pDb = NULL;
pDb = acdbHostApplicationServices()->workingDatabase();
if (!pDb) return;
ads_name ename;
ads_point pt;
if (acedEntSel(_T("\nSelect Xref: "), ename, pt) != RTNORM) return;
AcDbObjectId id = AcDbObjectId::kNull;
acdbGetObjectId(id, ename);
if (id == AcDbObjectId::kNull) return;
AcDbBlockReference *pRef = NULL;
if (acdbOpenAcDbObject((AcDbObject *&)pRef, id, AcDb::kForWrite) != Acad::eOk) return;
if (!pRef->isKindOf(AcDbBlockReference::desc()))
{
pRef->close();
return;
}
AcDbBlockTableRecord *pRec = NULL;
if (acdbOpenAcDbObject((AcDbObject*&)pRec, pRef->blockTableRecord(), AcDb::kForWrite) == Acad::eOk)
{
AcDbObjectId xrefBlkId;
ACHAR *pFilename, *pBlockName = _T("XREF1"); // falta definir el blocktable de pBlockname y hacer lock el dibujo

AcDbObjectId curSpaceId = pDb->currentSpaceId(); //acdbCurDwg()->currentSpaceId();


AcDbBlockTable *pBlockTable;
pDb->getSymbolTable(pBlockTable, AcDb::kForWrite);

AcDbObjectId pOutputId;
AcDbBlockTableRecord *pBlkRec = NULL;
if (acdbOpenObject(pBlkRec,curSpaceId,AcDb::kForWrite) == Acad::eOk)
{
AcDbBlockReference *pBlkRef = new AcDbBlockReference;

pBlkRec->appendAcDbEntity(pOutputId, pBlkRef);

pBlkRec->setName(_T("XREF1"));
pBlkRec->close();
pBlkRef->close();
}

if (pRec->pathName(pFilename) == Acad::eOk)
{
if (acdbOverlayXref(pDb, pFilename, pBlockName, xrefBlkId) == Acad::eOk)
acutPrintf(_T("\nDone..."));
}
pRec->close();
}
pRef->close();

//acdbOverlayXref()
//acdbAttachXref()
}

LE

  • Guest
Re: Something about Xref functions...
« Reply #8 on: December 21, 2006, 08:38:27 PM »
Here is the function working now... it does not update the nice icon on the palette, but... :)

Code: [Select]
        [CommandMethod("OVERLAY")]
        public void attachTooverlay()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            PromptEntityResult res = ed.GetEntity("\nSelect xref: ");
            if (res.Status != PromptStatus.OK) return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                ObjectId objId = res.ObjectId;
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                DBObject dbObj = tr.GetObject(objId, OpenMode.ForRead);
                BlockReference blkRef = dbObj as BlockReference;
                if (blkRef != null)
                {
                    BlockTableRecord blkDef = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForWrite);
                    if (blkDef.IsFromOverlayReference == false)
                    {
                        db.DetachXref(blkDef.ObjectId);

                        string fileName, blockName;
                        fileName = blkDef.PathName;
                        blockName = blkDef.Name;
                        ed.WriteMessage("\nPath: " + fileName);
                        ed.WriteMessage("\nBlockname: " + blockName);
                        doc.LockDocument();
                        ObjectId xrefId = db.OverlayXref(fileName, blockName);

                        BlockTableRecord blkRec = (BlockTableRecord)tr.GetObject(xrefId, OpenMode.ForRead);
                        BlockTableRecord BTR = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        BlockReference newBlkRef;
                        newBlkRef = new BlockReference(Point3d.Origin, blkRec.ObjectId);
                        BTR.AppendEntity(newBlkRef);
                        tr.AddNewlyCreatedDBObject(newBlkRef, true);

                        db.ResolveXrefs(true, true);

                    }
                }
                tr.Commit();
            }
        }

HTH

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Something about Xref functions...
« Reply #9 on: December 22, 2006, 01:36:03 AM »
Hi Luis,
I'm running past and can't really stop ... :-)

Are you sure you are using the
doc.LockDocument();
correctly ...

I thought it was only needed for commands in the application context
... and required the return value saved so that it could be disposed later.

ie

DocumentLock docLock = doc.LockDocument();
// bla, bla
docLock.Dispose();


... but I may be misunderstanding ....

/// regards, kwb
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Something about Xref functions...
« Reply #10 on: December 22, 2006, 05:45:30 AM »
... or if needed, perhaps wrap the DocumentLock in a using statement block .... ?


wish I had time to play. !!
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

LE

  • Guest
Re: Something about Xref functions...
« Reply #11 on: December 22, 2006, 11:18:21 AM »
... or if needed, perhaps wrap the DocumentLock in a using statement block .... ?


wish I had time to play. !!

Hi Kerry;

Actually is not needed, because of the using block... I forgot to remove it, it was part of previous tryouts, as you know in the objectarx acdbOverlayXref() function, they recommend to provide the locking, but as I understood in C# we can take advantage of using, as it is here, but wait for the masters to comfirm it.

I know how you feel.... :)


Thanks!

cadpro

  • Guest
Re: Something about Xref functions...
« Reply #12 on: November 02, 2008, 08:01:03 AM »
Is there a way to get the xrefs in the drawing that are inserted into the layout, and then chage them to overlay?

sinc

  • Guest
Re: Something about Xref functions...
« Reply #13 on: November 02, 2008, 10:10:27 AM »
The document lock should be unnecessary, since you are in a command method.

If you were to use it, though, you should remember the lock, and dispose of it when you no longer need the lock, as in Kerry's post.

cadpro

  • Guest
Re: Something about Xref functions...
« Reply #14 on: November 02, 2008, 11:12:53 PM »
I would like to know whether I could loop through the layout entities and get the xrefs inserted in the layout only, rather than selecting.

Thanks