.NET

Reply
Valued Contributor
mnav
Posts: 58
Registered: ‎12-15-2009
Message 1 of 3 (390 Views)
Accepted Solution

Attach PDF or RasterImage Reload

390 Views, 2 Replies
07-18-2011 08:47 AM

Hi

  Is there any Functionality available for Reloading the PDF or Image like we have for DWGs (Database.ReloadXrefs).

Thanks

Both PdfDefinition and RasterImageDef class has Load() and Unload() methods (their signature are slightly different, though).

 

Take PdfDefinition as example. To reload unloaded Pdf underlay, you can loop through all Pdfdefinitions in a drawing, and test each PdfDefinition.Loaded property. If Loaded=False, then you can call PdfDefinition.Load() method to reload it. Of course, before call Load() to reload, you may want to check PdfDefinition.SourceFileName/ActiveFileName to make sure the source file still exists. If not, you may want to prompt user and/or remove the PdfDefinition and/or locate the source file and reattach it to the drawing as underlay.

 

To get all PdfDefinition in a drawing, I used code like this:

 

 

private ObjectIdCollection GetPDFUnderlayDefs(Transaction tran)
{
ObjectIdCollection ids = new ObjectIdCollection();
DBDictionary namedDic = (DBDictionary)tran.GetObject(_db.NamedObjectsDictionaryId, OpenMode.ForRead);
string pdfDicKey = PdfDefinition.GetDictionaryKey(typeof(PdfDefinition));
if (pdfDicKey != null)
{
if (namedDic.Contains(pdfDicKey))
{
DBDictionary pdfDic = (DBDictionary)tran.GetObject(namedDic.GetAt(pdfDicKey), OpenMode.ForRead);
foreach (DBDictionaryEntry entry in pdfDic)
{
ObjectId id = (ObjectId)entry.Value;
ids.Add(id);
}
}
}
return ids;
}

 Then, I use this code to get unloaded PdfDefinitions (ObjectIdCollection):

 

public ObjectIdCollection GetUnloadedPdfDefs()
{
ObjectIdCollection ids = new ObjectIdCollection();
using (_dwg.LockDocument())
{
using (Transaction tran = _db.TransactionManager.StartTransaction())
{
ObjectIdCollection defs = GetPDFUnderlayDefs(tran);
foreach (ObjectId id in defs)
{
PdfDefinition def =(PdfDefinition) tran.GetObject(id, OpenMode.ForRead);
if (!def.Loaded) ids.Add(id);
}
tran.Commit();
}
}
return ids;
}

 Then I can do reload by passing the ObjectIdCollection obtained from GetUnloadedPdfDefs():

public void ReloadPdf(ObjectIdCollection pdfdefIds)
{
using (_dwg.LockDocument())
{
using (Transaction tran = _db.TransactionManager.StartTransaction())
{
foreach (ObjectId id in pdfdefIds)
{
PdfDefinition def = (PdfDefinition)tran.GetObject(id, OpenMode.ForWrite);
if (!def.Loaded) def.Load(null);
}
tran.Commit();
}
}
}

 After calling ReloadPdf(), you would need to call Editor.Regen() to make the reloaded Pdf underlay to show up on screen.

 

*Expert Elite*
norman.yuan
Posts: 1,063
Registered: ‎04-27-2009
Message 2 of 3 (368 Views)

Re: Attach PDF or RasterImage Reload

07-18-2011 02:38 PM in reply to: mnav

Both PdfDefinition and RasterImageDef class has Load() and Unload() methods (their signature are slightly different, though).

 

Take PdfDefinition as example. To reload unloaded Pdf underlay, you can loop through all Pdfdefinitions in a drawing, and test each PdfDefinition.Loaded property. If Loaded=False, then you can call PdfDefinition.Load() method to reload it. Of course, before call Load() to reload, you may want to check PdfDefinition.SourceFileName/ActiveFileName to make sure the source file still exists. If not, you may want to prompt user and/or remove the PdfDefinition and/or locate the source file and reattach it to the drawing as underlay.

 

To get all PdfDefinition in a drawing, I used code like this:

 

 

private ObjectIdCollection GetPDFUnderlayDefs(Transaction tran)
        {
            ObjectIdCollection ids = new ObjectIdCollection();

            DBDictionary namedDic = (DBDictionary)tran.GetObject(_db.NamedObjectsDictionaryId, OpenMode.ForRead);
    

            string pdfDicKey = PdfDefinition.GetDictionaryKey(typeof(PdfDefinition));
            if (pdfDicKey != null)
            {
                if (namedDic.Contains(pdfDicKey))
                {
                    DBDictionary pdfDic = (DBDictionary)tran.GetObject(namedDic.GetAt(pdfDicKey), OpenMode.ForRead);
                    foreach (DBDictionaryEntry entry in pdfDic)
                    {
                        ObjectId id = (ObjectId)entry.Value;
                        ids.Add(id);
                    }
                }
            }
                    
            return ids;
        }

 Then, I use this code to get unloaded PdfDefinitions (ObjectIdCollection):

 

public ObjectIdCollection GetUnloadedPdfDefs()
        {
            ObjectIdCollection ids = new ObjectIdCollection();

            using (_dwg.LockDocument())
            {
                using (Transaction tran = _db.TransactionManager.StartTransaction())
                {
                    ObjectIdCollection defs = GetPDFUnderlayDefs(tran);

                    foreach (ObjectId id in defs)
                    {
                        PdfDefinition def =(PdfDefinition) tran.GetObject(id, OpenMode.ForRead);
                        if (!def.Loaded) ids.Add(id);
                    }

                    tran.Commit();
                }
            }

            return ids;
        }

 Then I can do reload by passing the ObjectIdCollection obtained from GetUnloadedPdfDefs():

public void ReloadPdf(ObjectIdCollection pdfdefIds)
        {
            using (_dwg.LockDocument())
            {
                using (Transaction tran = _db.TransactionManager.StartTransaction())
                {
                    foreach (ObjectId id in pdfdefIds)
                    {
                        PdfDefinition def = (PdfDefinition)tran.GetObject(id, OpenMode.ForWrite);
                        if (!def.Loaded) def.Load(null);
                    }

                    tran.Commit();
                }
            }
        }

 After calling ReloadPdf(), you would need to call Editor.Regen() to make the reloaded Pdf underlay to show up on screen.

 

Valued Contributor
mnav
Posts: 58
Registered: ‎12-15-2009
Message 3 of 3 (356 Views)

Re: Attach PDF or RasterImage Reload

07-19-2011 02:05 AM in reply to: norman.yuan

Thanks norman.yuan for your help. i was calling the load function but missing the regen of drwing. that solved the problem.

Thanks a lot once again.

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.