.NET

Reply
New Member
x2dalyon
Posts: 2
Registered: ‎02-13-2012
Message 1 of 3 (1,034 Views)

How to embed PDF OLE object?

1034 Views, 2 Replies
02-13-2012 12:38 PM

I do not see a method on the ModelSpace object that will allow me to embed a PDF or other file as an OLE object.  Can someone point me to an example?

ADN Support Specialist
philippe.leefsma
Posts: 639
Registered: ‎06-02-2009
Message 2 of 3 (1,014 Views)

Re: How to embed PDF OLE object?

02-14-2012 05:01 AM in reply to: x2dalyon

Pdf can be attached to drawing as follow:

 

[CommandMethod("PdfNetAttach")]

static public void PdfNetAttach()

{

    Document doc = Application.DocumentManager.MdiActiveDocument;

    Database db = doc.Database;

    Editor ed = doc.Editor;

 

    using (Transaction Tx = db.TransactionManager.StartTransaction())

    {

        DBDictionary nod = Tx.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary;

        string defDictKey = UnderlayDefinition.GetDictionaryKey(typeof(PdfDefinition));

 

        if (!nod.Contains(defDictKey))

        {

            nod.UpgradeOpen();

 

            using (DBDictionary dict = new DBDictionary())

            {

                nod.SetAt(defDictKey, dict);

                Tx.AddNewlyCreatedDBObject(dict, true);

            }

        }

 

        ObjectId idPdfDef;

        DBDictionary pdfDict = Tx.GetObject(nod.GetAt(defDictKey), OpenMode.ForWrite) as DBDictionary;

 

        using (PdfDefinition pdfDef = new PdfDefinition())

        {

            pdfDef.SourceFileName = "C:\\Temp\\new.pdf";

            idPdfDef = pdfDict.SetAt("TESTPDF", pdfDef);

            Tx.AddNewlyCreatedDBObject(pdfDef, true);

        }

 

        BlockTable bt = Tx.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

        BlockTableRecord btr = Tx.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

               

        using (PdfReference pdf = new PdfReference())

        {

            pdf.DefinitionId = idPdfDef;

 

            btr.AppendEntity(pdf);

            Tx.AddNewlyCreatedDBObject(pdf, true);

        }

 

        Tx.Commit();

    }

}

 

Unfortunately it is not possible to create an AcDbOle2Frame object. The reason can be found in the ObjectARX help file under the topic setOleObject():

 

The COleClientItem class used by AutoCAD is a modified version of the class that ships from Microsoft. This means that it is not possible for applications to create their own COleClientItem objects for use by this function. This function can only be used with COleClientItem objects created by AutoCAD (that is, the object from another AcDbOle2Frame object).

 

I have one workaround to suggest (though not very elegant, but just might be of help):

 

Create a drawing with a OLE embedded object created using the _insertobj command in it. This drawing will become a "ole template" for creating the OLE objects, hence should be available to the ARX program.

 

Programmatically insert and explode the "ole template" drawing in current drawing.

 

You can get the pointer to the AcDbOle2Frame object in the exploded insert. Once you have the pointer, it is easy to modify the objects propertie s(like pointing to a different file, changing the position etc).”

 

Here is how you could insert an ole2Frame in your drawing, based on an existing template:

 

 

[CommandMethod("InsertOle")]

public static void InsertOle()

{

    Document doc = Application.DocumentManager.MdiActiveDocument;

    Database db = doc.Database;

    Editor ed = doc.Editor;

 

    Database refDb = new Database();

    refDb.ReadDwgFile("c:\\Temp\\MyOleTemplate.dwg", System.IO.FileShare.Read, true, "");

 

    using (Transaction Tx = db.TransactionManager.StartTransaction())

    {

        BlockTable bT = Tx.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;

        BlockTableRecord bTR = Tx.GetObject(bT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

 

        ObjectId btrId = db.Insert("MyOleTemplate", refDb, true);

 

        BlockReference blockRef = new BlockReference(new Point3d(), btrId);

 

        DBObjectCollection entitySet = new DBObjectCollection();

        blockRef.Explode(entitySet);

 

        foreach (object obj in entitySet)

        {

            if (obj is Ole2Frame)

            {

                Ole2Frame ole = obj as Ole2Frame;

 

                //...

 

                bTR.AppendEntity(ole);

                Tx.AddNewlyCreatedDBObject(ole, true);

            }

        }

 

        Tx.Commit();

    }

}

 

Regards,

Philippe.

 

Philippe Leefsma
Developer Consultant
Developer Technical Services
Global Subscription & Support

 

Autodesk EMEA

 

www.autodesk.com/joinadn



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

Member
boughannam
Posts: 5
Registered: ‎11-25-2007
Message 3 of 3 (498 Views)

Re: How to embed PDF OLE object?

05-15-2013 08:45 AM in reply to: x2dalyon

Does this code work if the drawing is not openend and read as a db. Below is the modified code with drawing read as database. The OLE is inserted in the OLE links but it is not visible. Any ideas.

 

 

[CommandMethod("InsertOle")]

       

publicstaticvoid InsertOle()

        {

          

Database db = newDatabase(false, true);

            db.ReadDwgFile("C:\\Data\\Drawing 2.dwg", FileShare.ReadWrite, false, "");

   

           

           

Database refDb = newDatabase();

            refDb.ReadDwgFile("C:\\Data\\OLE Template.dwg", System.IO.FileShare.Read, true, "");

 

 

           

using (Transaction Tx = db.TransactionManager.StartTransaction())

            {

               

BlockTable bT = Tx.GetObject(db.BlockTableId, OpenMode.ForWrite) asBlockTable;

               

BlockTableRecord bTR = Tx.GetObject(bT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) asBlockTableRecord;

               

               

               

ObjectId btrId = db.Insert("MyOleTemplate", refDb, true);

               

BlockReference blockRef = newBlockReference(newPoint3d(), btrId);

               

DBObjectCollection entitySet = newDBObjectCollection();

                blockRef.Explode(entitySet);

 

 

               

foreach (object obj in entitySet)

                {

                   

if (obj isOle2Frame)

                    {

                       

Ole2Frame ole = obj asOle2Frame;               

                

                        bTR.AppendEntity(ole);

                        Tx.AddNewlyCreatedDBObject(ole,

true);

                    }

                }

 

                Tx.Commit();

                db.SaveAs(

"C:\\Data\\Drawing 2.dwg", false, DwgVersion.Current, db.SecurityParameters);

            }

        }

Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.