How to import an image into a 2013/ 2018 .dwg using C#

Hello Briccad devs,

I need to import a previously captured image into a certain .dwg drawing.
I have the Drawing ready and the Image waiting on my disc.
Can you please suggest a proper way to do this?
Thank you very much for your effort and time.

I have tried the following:

This approach gets me stuck at loadin an image from disc. I can not figure out why. Perhaps you know a better way.
Stackoverflow - How to import an image into a 2013/ 2018 .dwg using C#?

I also tried this

This approach crashes at: "_AcDb.ObjectId objectId = pDatabase.AttachXref(pFilePath, pName);"

public void XrefAttachAtOrigin(_AcAp.Document pDocument, string pFilePath) {
    bool result = XrefAttachAndInsert(pDocument.Database, pFilePath, _AcGe.Point3d.Origin);
    m_communicator.BCUtils.WriteLine("External reference" + (result ? "was" : "was not ") + "attached at the origin.");
}

public bool XrefAttachAndInsert(_AcDb.Database pDatabase, string pFilePath, _AcGe.Point3d pPosition, string pName = null) {
    var result = false;

    if (!File.Exists(pFilePath)) {
        m_communicator.BCUtils.WriteLine("XrefAttachAtOrigin: FilePath does not exist.");
        return result;
    }

    if (String.IsNullOrEmpty(pName))
    pName = Path.GetFileNameWithoutExtension(pFilePath);

    m_communicator.BCUtils.WriteLine("XrefAttachAtOrigin: pName == " + pName);

    try {
        using (_AcDb.Transaction transaction = pDatabase.TransactionManager.StartOpenCloseTransaction()) {
            _AcDb.ObjectId objectId = pDatabase.AttachXref(pFilePath, pName);
            if (objectId.IsValid) {
                using (_AcDb.BlockTableRecord blockTableRecord = transaction.GetObject(pDatabase.CurrentSpaceId, _AcDb.OpenMode.ForWrite) as _AcDb.BlockTableRecord)
                using (_AcDb.BlockReference blockReference = new _AcDb.BlockReference(pPosition, objectId)) {
                    if (blockReference != null) {
                        blockTableRecord.AppendEntity(blockReference);
                        transaction.AddNewlyCreatedDBObject(blockReference, true);
                        result = true;
                    }
                    else {
                        m_communicator.BCUtils.WriteLine("XrefAttachAtOrigin: BlockReference == null");
                    }
                }
            }
            else {
                m_communicator.BCUtils.WriteLine("XrefAttachAtOrigin: objectId.IsValid == false");
            }
            transaction.Commit();
        }
    }
    catch { }
    return result;
}

I also tried this

This approach works, but leaves me with an XReferenced image. I need an inserted image. XRef is not good enough.

    {
        Document.SendStringToExecute("-IMAGEATTACH\n", true, false, false);
        Document.SendStringToExecute("Full path\n", true, false, false);
        Document.SendStringToExecute(flatshotInfos[0].ScreenshotInfo.FilePath + "\n", true, false, false);
        Document.SendStringToExecute("0,0,0\n", true, false, false)
        Document.SendStringToExecute("\n", true, false, false);
        Document.SendStringToExecute("\n", true, false, false);
}

Comments

  • Terry Dotson
    edited December 2020

    I assume you mean attach a raster image as opposed to embedding one. I don't think embedding is possible without involving OLE, which has too many problems to list.

    There are lots of examples of attaching an image using .NET, search for the term "ImageDefId". It's sort of a two stage process, you add the definition first, then you add a reference "RasterImage" to the desired space (model vs paper).

    One other consideration inside BricsCAD is that it may (at least in early versions) require you to set the RasterImage.DisplayOptions and RasterImage.Show = True.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. Click one of the buttons on the top bar to get involved!