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

0 Members and 1 Guest are viewing this topic.

Bryco

  • Water Moccasin
  • Posts: 1850

T.Willey

  • Needs a day job
  • Posts: 5218
Re: Something about Xref functions...
« Reply #31 on: November 19, 2008, 10:58:45 AM »
Tim I had got that far ( the thing works) but I couldn't make the new dic=the old dic and get it to take.
I don't know if I will have time to play today, but I would think that you would have to clone the first item that the new entity doesn't have; mean if there is not extension dictionary, then clone that, but if that is there and the acad filters dictionary isn't there, then clone that.  If I have time to play, which would be cool, I'll see what I can do.
Tim

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

Please think about donating if this post helped you.

cadpro

  • Guest
Re: Something about Xref functions...
« Reply #32 on: November 20, 2008, 12:01:46 AM »
http://www.theswamp.org/index.php?topic=14465.msg174209#msg174209

I know how to loop through the layouts. But how do I get to the blocks in the layouts? And I think the code that I posted in the previous post is fair enough to loop through the xrefs in the drawing. Of these, is there a way to find out if the xref in inserted in model or layout?

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6980
  • AKA Daniel
Re: Something about Xref functions...
« Reply #33 on: November 20, 2008, 01:39:10 AM »
http://www.theswamp.org/index.php?topic=14465.msg174209#msg174209

I know how to loop through the layouts. But how do I get to the blocks in the layouts? And I think the code that I posted in the previous post is fair enough to loop through the xrefs in the drawing. Of these, is there a way to find out if the xref in inserted in model or layout?

Alexander Rivilis gave you the answer here http://www.theswamp.org/index.php?topic=14068.msg309109#msg309109



Bryco

  • Water Moccasin
  • Posts: 1850
Re: Something about Xref functions...
« Reply #34 on: November 20, 2008, 02:56:04 AM »
And another way is to use the blockid
BlockReference br = tr.GetObject
                    (TheBlockreference.ObjectId, OpenMode.ForWrite) as BlockReference;
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                if(br.BlockId==bt[BlockTableRecord.ModelSpace])
                    msg="modelspace";
                else
                    msg="paperspace";
                    MessageBox.Show(msg);

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6980
  • AKA Daniel
Re: Something about Xref functions...
« Reply #35 on: November 20, 2008, 03:45:01 AM »
Code: [Select]
namespace ExecMethod
{
  public class Commands
  {
    [CommandMethod("doit")]
    public static void doit()
    {
      AcEd.Editor editor = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
      try
      {
        AcDb.Database database = HostApplicationServices.WorkingDatabase;
        AcDb.TransactionManager transactionManager = database.TransactionManager;
        using (AcDb.Transaction transaction = transactionManager.StartTransaction())
        {

          DBDictionary LayoutDictionary = transaction.GetObject
            (database.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;

          foreach (AcDb.DBDictionaryEntry entry in LayoutDictionary)
          {
            if (!entry.Key.Equals("Model"))
            {

              AcDb.Layout layout = transaction.GetObject
                (entry.Value, OpenMode.ForRead) as AcDb.Layout;

              AcDb.BlockTableRecord blockTableRecord = transaction.GetObject
                (layout.BlockTableRecordId, OpenMode.ForRead) as AcDb.BlockTableRecord;

              foreach (AcDb.ObjectId objectId in blockTableRecord)
              {
                AcDb.BlockReference blockReference = transaction.GetObject
                              (objectId, OpenMode.ForRead) as AcDb.BlockReference;

                if (blockReference != null)
                {
                  editor.WriteMessage("\nBlockReference found {0} in layout {1}", 
                                                  blockReference.ObjectId, entry.Key);
                }
              }
            }
          }
        }
      }
      catch (System.Exception ex)
      {
       editor.WriteMessage("\n" + ex.Message);
      }
    }
  }
}

T.Willey

  • Needs a day job
  • Posts: 5218
Re: Something about Xref functions...
« Reply #36 on: November 20, 2008, 01:54:27 PM »
Tim I had got that far ( the thing works) but I couldn't make the new dic=the old dic and get it to take.
I don't know if I will have time to play today, but I would think that you would have to clone the first item that the new entity doesn't have; mean if there is not extension dictionary, then clone that, but if that is there and the acad filters dictionary isn't there, then clone that.  If I have time to play, which would be cool, I'll see what I can do.

This is what I came up with.  It reports that is worked, the message box shows object id numbers for the new dictionary and spatial object, but it won't update the xref with the xclip.  I don't think I will have anymore time today to play, but this is what I got so far.  Two new xrefs in a drawing.  Xclip'ed one, then run the command.

Code: [Select]
[CommandMethod( "TestCopyXClip" )]
public void TestCopyXClip() {
PromptEntityResult per;
ObjectId tempId;

Document Doc = acadApp.DocumentManager.MdiActiveDocument;
Database Db = Doc.Database;
Editor Ed = Doc.Editor;
using ( Transaction Trans = Db.TransactionManager.StartTransaction() ) {
per = Ed.GetEntity( "Select xref to copy xclip to: " );
Entity ToEnt = Trans.GetObject( per.ObjectId, OpenMode.ForWrite ) as Entity;

per = Ed.GetEntity( "Select xref to copy xclip from: " );
Entity FromEnt = Trans.GetObject( per.ObjectId, OpenMode.ForRead ) as Entity;

DBDictionary xDictFrom = Trans.GetObject( FromEnt.ExtensionDictionary, OpenMode.ForRead ) as DBDictionary;
tempId = xDictFrom.GetAt( "ACAD_FILTER" );
DBDictionary FltDict = Trans.GetObject( tempId, OpenMode.ForRead ) as DBDictionary;

ToEnt.CreateExtensionDictionary();
DBDictionary xDictTo = Trans.GetObject( ToEnt.ExtensionDictionary, OpenMode.ForWrite ) as DBDictionary;
IdMapping iMap = new IdMapping();
DBDictionary nFltDict = FltDict.DeepClone( xDictTo, iMap, true ) as DBDictionary;
xDictTo.SetAt( "ACAD_FILTER", nFltDict );
try {
tempId = xDictTo.GetAt( "ACAD_FILTER" );
MessageBox.Show( tempId.ToString() );
tempId = nFltDict.GetAt( "SPATIAL" );
MessageBox.Show( tempId.ToString() );
}
catch ( System.Exception e ) { MessageBox.Show( e.Message + Environment.NewLine + e.StackTrace ); }
Trans.Commit();
}
}
Tim

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

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5218
Re: Something about Xref functions...
« Reply #37 on: November 20, 2008, 01:59:42 PM »
If you want to see what space the xref is inserted in, then just step through all the ObjectId's in the ObjectIdCollection returned by GetBlockReferenceIds method of the BlockTableRecord associated with the xref.  You can get the BlockTableRecord of the OwnerId of the BlockReference, and then see if it's a layout, and if so which layout it is in.

So of my code ( I would do it differently now, but don't have time right now ).  btr = BlockTableRecord of the Xref.

Code: [Select]
ObjectIdCollection ObjIdCol = (ObjectIdCollection)btr.GetBlockReferenceIds(true, true);
for (int j = 0; j < ObjIdCol.Count; ++j) {
    ObjectId ObjId = ObjIdCol[j];
    BlockReference BlkRef = (BlockReference)Trans.GetObject(ObjId, OpenMode.ForRead);
    BlockTableRecord tempbtr = (BlockTableRecord)Trans.GetObject(BlkRef.OwnerId, OpenMode.ForRead);
    if (tempbtr.IsLayout)
        Layout templo = (Layout)Trans.GetObject(tempbtr.LayoutId, OpenMode.ForRead);
}

Tim

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

Please think about donating if this post helped you.

Bryco

  • Water Moccasin
  • Posts: 1850
Re: Something about Xref functions...
« Reply #38 on: November 20, 2008, 06:20:58 PM »
Tim does  db.DeepCloneObjects do any better?
I'll look tonight. When I made the new dic=olddic it also reported everything is ok.

T.Willey

  • Needs a day job
  • Posts: 5218
Re: Something about Xref functions...
« Reply #39 on: November 20, 2008, 06:26:25 PM »
Tim does  db.DeepCloneObjects do any better?
Not with my testing Bryco.  I'm still trying some stuff, but nothing good is happening.

I'll look tonight. When I made the new dic=olddic it also reported everything is ok.
Sounds good.  Hope you have better luck than I am.
Tim

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

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5218
Re: Something about Xref functions...
« Reply #40 on: November 20, 2008, 07:03:46 PM »
I have put in a request for help with ADN.  Lets see how helpful they are this time.  *Not getting my hopes up too high.*
Tim

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

Please think about donating if this post helped you.

kdub

  • Mesozoic relic
  • SuperMod
  • Swamp Rat
  • Posts: 1370
  • class keyThumper<T>:ILazy<T>
Re: Something about Xref functions...
« Reply #41 on: November 20, 2008, 07:33:51 PM »
Tim,
You could also send an email to Kean Walmsley .. he may follow up the ADN request ..
called Kerry in my other life

Sometimes the question is more important than the answer.
#ridesober

T.Willey

  • Needs a day job
  • Posts: 5218
Re: Something about Xref functions...
« Reply #42 on: November 20, 2008, 07:41:34 PM »
Tim,
You could also send an email to Kean Walmsley .. he may follow up the ADN request ..
Good idea Kerry.  I'll see if they send anything out tomorrow.
Tim

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

Please think about donating if this post helped you.

Bryco

  • Water Moccasin
  • Posts: 1850
Re: Something about Xref functions...
« Reply #43 on: November 20, 2008, 11:59:54 PM »
Well Tim DeepClone seems to be the GO.
Both a regen and a AddNewlyCreatedDBObject are required to make it work.
SetAt must add it to the database.
So I guess the answer is a new dictionary is requied.

Code: [Select]
[CommandMethod("TestCopyXClip")]


public void TestCopyXClip()
{
    PromptEntityResult per;
    ObjectId tempId;

    Document Doc = acadApp.DocumentManager.MdiActiveDocument;
    Database Db = Doc.Database;
    Editor Ed = Doc.Editor;
    using (Transaction Trans = Db.TransactionManager.StartTransaction())
    {
        per = Ed.GetEntity("Select xref to copy xclip to: ");
        Entity ToEnt = Trans.GetObject(per.ObjectId, OpenMode.ForWrite) as Entity;
       

        per = Ed.GetEntity("Select xref to copy xclip from: ");
        Entity FromEnt = Trans.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;

        DBDictionary xDictFrom = Trans.GetObject(FromEnt.ExtensionDictionary, OpenMode.ForRead) as DBDictionary;
        tempId = xDictFrom.GetAt("ACAD_FILTER");
        DBDictionary FltDict = Trans.GetObject(tempId, OpenMode.ForRead) as DBDictionary;
        ObjectId xclipId = ToEnt.ExtensionDictionary;
        if(xclipId.IsNull)ToEnt.CreateExtensionDictionary();
       
        DBDictionary xDictTo = Trans.GetObject(ToEnt.ExtensionDictionary, OpenMode.ForWrite) as DBDictionary;
        IdMapping iMap = new IdMapping();
        DBDictionary nFltDict = FltDict.DeepClone(xDictTo, iMap, true) as DBDictionary;
        xDictTo.SetAt("ACAD_FILTER", nFltDict);

        Trans.AddNewlyCreatedDBObject(nFltDict, true);
        Db.ResolveXrefs(true, true);
        Ed.Regen();
        Trans.Commit();
    }
}

T.Willey

  • Needs a day job
  • Posts: 5218
Re: Something about Xref functions...
« Reply #44 on: November 21, 2008, 11:14:19 AM »
Well Tim DeepClone seems to be the GO.
Both a regen and a AddNewlyCreatedDBObject are required to make it work.
SetAt must add it to the database.
So I guess the answer is a new dictionary is requied.

<snip>

Good catch Bryco!  It didn't even occur to me to use AddNewlyCreatedDBObject, but not it seems so simple.   :oops:  Thanks for posting that.  I was loosing my mine here, not being able to understand why it wasn't working.  I have some more complete code here.  Let me update it per this, and I'll post it.
Tim

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

Please think about donating if this post helped you.