Civil 3D Iterating through Xref RasterImage list, loading to Clipboard and pasting OLE

I’m developing a graph to get all linked images, load them into the Windows clipboard and paste into the current document using the system API.

I can get it working on single Xrefs, however when I try to iterate over a list it only pastes the last image multiple times.

Here is the code using RasterImage.Path (index 0) and RasterImage.Orientation.Origin (index 3)

import sys
import clr
import os

# Add Assemblies for AutoCAD
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *
# Remove conflicting method
del globals()["Image"]

adoc = Application.DocumentManager.MdiActiveDocument

# Add Assemblies for Windows
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

# Import references from Windows
from System.Windows.Forms import Clipboard
from System.Drawing import Image

output = []

for raster in IN[0]:
    imgpath = raster[0]
    if isinstance(imgpath, str) and os.path.isfile(imgpath):
        img = Image.FromFile(imgpath)
        Clipboard.Clear()
        Clipboard.SetImage(img)
        
        if Clipboard.ContainsImage():
            origin = raster[3]
            comstr = "_.PASTECLIP\n" + str(origin.X) + "," + str(origin.Y) + "\n"
            
            adoc.SendStringToExecute(comstr, True, False, True)
        
            output.append([True, raster[3:]])
      
        else:
            output.append([False])        
    else:
        output.append([False])
        
OUT = output

What I am thinking is that the SendStringToExecute is being queued while the python script and system calls are being run and then executed as the loop or graph completes. I also tried time.sleep() in the loop and a custom node with longest lacing to no avail.

Ed: It appears SendStringToExecute is asynchronous and the synchronous editor.Command() keeps throwing an exception

Can anyone assist with Dynamo list loop that can push items through a sub-graph one at a time or any API calls that can synchronously send a command?

Solved using Autodesk.AutoCAD.Interop.dll

clr.AddReference('Autodesk.AutoCAD.Interop')
from Autodesk.AutoCAD.Interop import *
app = Application.AcadApplication
...
app.ActiveDocument.SendCommand(comstr)

Hello, could you share the final script?

import sys
import clr
import os

# Add Assemblies for AutoCAD
clr.AddReference("AcMgd")
clr.AddReference("AcCoreMgd")
clr.AddReference("AcDbMgd")
clr.AddReference("Autodesk.AutoCAD.Interop")

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *
from Autodesk.AutoCAD.Interop import *

# Remove conflicting AutoCAD.Geometry method
del globals()["Image"]

adoc = Application.DocumentManager.MdiActiveDocument
app = Application.AcadApplication

# Add Assemblies for Windows
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

# Import references from Windows
from System.Windows.Forms import Clipboard
from System.Drawing import Image

output = []

for raster in IN[0]:
    imgpath = raster[0]
    if isinstance(imgpath, str) and os.path.isfile(imgpath):
        img = Image.FromFile(imgpath)
        Clipboard.Clear()
        Clipboard.SetImage(img)

        if Clipboard.ContainsImage():
            origin = raster[3]
            comstr = "_.PASTECLIP\n" + str(origin.X) + "," + str(origin.Y) + "\n"

            app.ActiveDocument.SendCommand(comstr)

            output.append([True, raster[3:]])

        else:
            output.append([False])
    else:
        output.append([False])

OUT = output

hello, where is the error?

Hi @Angel76
There is not enough information to give a definite answer - I originally posted this code as I was unable to get the clipboard paste working within a script
Are the selected objects raster images?
What is the result get parameter by name - is it a file path to an image?
The script returns false if there is no image file or the clipboard does not contain an image
Do you have clipboard history turned on? This will not work with clipboard history

I have images linked as external references (Raster), I want them to be embedded (OLE).
Link of the files, thanks for the help

I can’t help you further here,
You will have to do investigation and testing yourself
It is possible to paste image OLEs from raster objects and my code is proof of concept
Note that the Raster Object path can be out of date - is the folder and the image files in the folder specified in the path
Good luck

1 Like

Do you have an example of how your code works please, it would be very helpful, I don’t use python code, I’m an apprentice, thanks