MapiInsert using c#

MapiInsert using c#

mchan01
Contributor Contributor
1,375 Views
5 Replies
Message 1 of 6

MapiInsert using c#

mchan01
Contributor
Contributor

Is there a may to user MapIInser command in c#?

 

I would want to use the command and automatically download an image file i create. I wanted to skip the file selection mode.

 

Thanks

0 Likes
1,376 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable

Hi,

 

I had to replace an existing image with a new one in multiple drawings. I didn't find an "mapiinsert" function in Map API.

Here is what I used at the end:

 

        public static void win_imageinsert(Database openedFileDB, bool backgroundTransparent)
        {

            /*
            Database acCurDb = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            Document curDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            */

            Database acCurDb = openedFileDB;
            Editor ed = UtilCadActive.Editor;
            Document curDoc = UtilCadActive.Document;






            using (DocumentLock docLock = curDoc.LockDocument())
            {

                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    // Define the name and image to use
                    string strImgName = dicImageInsertDetails["imagename"];
                    string strFileName = dicImageInsertDetails["sourcename"];

                    RasterImageDef acRasterDef;
                    bool bRasterDefCreated = false;
                    ObjectId acImgDefId;

                    // Get the image dictionary
                    ObjectId acImgDctID = RasterImageDef.GetImageDictionary(acCurDb);

                    // Check to see if the dictionary does not exist, it not then create it
                    if (acImgDctID.IsNull)
                    {
                        acImgDctID = RasterImageDef.CreateImageDictionary(acCurDb);
                    }

                    // Open the image dictionary
                    DBDictionary acImgDict = acTrans.GetObject(acImgDctID, OpenMode.ForRead) as DBDictionary;

                    // Check to see if the image definition already exists
                    if (acImgDict.Contains(strImgName))
                    {
                        acImgDefId = acImgDict.GetAt(strImgName);

                        acRasterDef = acTrans.GetObject(acImgDefId, OpenMode.ForWrite) as RasterImageDef;
                    }
                    else
                    {
                        // Create a raster image definition
                        RasterImageDef acRasterDefNew = new RasterImageDef();

                        //--> robert
                        //acRasterDefNew.ResolutionMMPerPixel = new Vector2d(312.5, 312.5);
                        //acRasterDefNew.ResolutionUnits = Unit.Meter;
                        // Set the source for the image file
                        acRasterDefNew.SourceFileName = strFileName;
                        



                        // Load the image into memory
                        acRasterDefNew.Load();
                        //ed.WriteMessage("\nResMMPerPixel = " + acRasterDefNew.ResolutionMMPerPixel);
                        string x_y = dicImageInsertDetails["resmmperpixel_x_y"];
                        string[] xy = x_y.Split(',');
                        double x, y;
                        bool b = double.TryParse(xy[0], out x);
                        b = double.TryParse(xy[1], out y);


                        acRasterDefNew.ResolutionMMPerPixel = new Vector2d(x, y);
                        //ed.WriteMessage("\nResMMPerPixel = " + acRasterDefNew.ResolutionMMPerPixel);
                        acRasterDefNew.ResolutionUnits = Unit.Meter;
                        // Add the image definition to the dictionary
                        acImgDict.UpgradeOpen();
                        acImgDefId = acImgDict.SetAt(strImgName, acRasterDefNew);



                        acTrans.AddNewlyCreatedDBObject(acRasterDefNew, true);

                        acRasterDef = acRasterDefNew;



                        bRasterDefCreated = true;
                    }

                    // Open the Block table for read
                    BlockTable acBlkTbl;
                    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

                    // Open the Block table record Model space for write
                    BlockTableRecord acBlkTblRec;
                    acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                    
                    // Create the new image and assign it the image definition
                    using (RasterImage acRaster = new RasterImage())
                    {
                        acRaster.ImageDefId = acImgDefId;

                        string layername = dicImageInsertDetails["layer"];



                        LayerTable lt = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;

                        if (lt.Has(layername))
                        {

                            acRaster.Layer = dicImageInsertDetails["layer"];
                        }
                        else
                        {
                            //WriteMessage("\nlayer not found: "+layername);
                        }



                        // Use ImageWidth and ImageHeight to get the size of the image in pixels (1024 x 768).
                        // Use ResolutionMMPerPixel to determine the number of millimeters in a pixel so you 
                        // can convert the size of the drawing into other units or millimeters based on the 
                        // drawing units used in the current drawing.


                        // Define the position for the image 


                        string x_y = dicImageInsertDetails["insertionpoint"];
                        string[] xy = x_y.Split(',');
                        double x, y;
                        bool b = double.TryParse(xy[0], out x);
                        b = double.TryParse(xy[1], out y);

                        Point3d insPt = new Point3d(x, y, 0.0);


                        string res = dicImageInsertDetails["resmperpixel_x"];
                        double re;
                        b = double.TryParse(res, out re);




                        Vector3d u = new Vector3d(acRaster.ImageWidth * re, 0, 0);
                        Vector3d v = new Vector3d(0, acRaster.ImageHeight * re, 0);


                        acRasterDef.ResolutionUnits = Unit.Meter;

                        acRaster.Orientation = new CoordinateSystem3d(insPt, u, v);

                        /*
                        ed.WriteMessage("\nOrientation = " + acRaster.Orientation.ToString());
                        ed.WriteMessage("\nu /v vector = " + u.ToString() + " - " + v.ToString());
                        ed.WriteMessage("\nResMMPerPixel = " + acRasterDef.ResolutionMMPerPixel);
                        ed.WriteMessage("\nRasterDefSize = " + acRasterDef.Size);
                        ed.WriteMessage("\nResolutionUnits = " + acRasterDef.ResolutionUnits);
                        */
                        acRasterDef.ResolutionUnits = Unit.Meter;


                        // Set the rotation angle for the image
                        acRaster.Rotation = 0;

                        if(backgroundTransparent)
                            acRaster.DisplayOptions = ImageDisplayOptions.Transparent;

                        // Add the new object to the block table record and the transaction
                        acBlkTblRec.AppendEntity(acRaster);
                        acTrans.AddNewlyCreatedDBObject(acRaster, true);

                        // Connect the raster definition and image together so the definition
                        // does not appear as "unreferenced" in the External References palette.
                        RasterImage.EnableReactors(true);
                        acRaster.AssociateRasterDef(acRasterDef);


                        //Draw order - to bottom
                        //Entity ent = acTrans.GetObject(acRaster.ObjectId, OpenMode.ForRead) as Entity;
                        //BlockTableRecord block = acTrans.GetObject(acRaster.ObjectId, OpenMode.ForRead) as BlockTableRecord;

                        //dRAWoRDER
                        DrawOrderTable dot = acTrans.GetObject(acBlkTblRec.DrawOrderTableId, OpenMode.ForWrite) as DrawOrderTable;

                        ObjectIdCollection objToMove = new ObjectIdCollection();
                        objToMove.Add(acRaster.ObjectId);
                        //objToMove.Add(acRasterDef.ObjectId);
                        dot.MoveToBottom(objToMove);
                        acRaster.ColorIndex = 256;


                      

                       

                        if (bRasterDefCreated)
                        {
                            acRasterDef.Dispose();
                        }
                    }

                    // Save the new object to the database
                    acTrans.Commit();

                    // Dispose of the transaction
                }
            }
        }
0 Likes
Message 3 of 6

Anonymous
Not applicable

Hello,

 

with a script this is fairly easy to do.

 

First you´ll need to establish FILEDIA = 0, in order to avoid dialogue windows.

 

The script is a simple textfile with extension *.scr and contains the following lines:

 

MAPIINSERT
D:\Image.ecw
N

 

Just execute the command MAPIINSERT once manually and you´ll understand the lines of the script.

 

 

It should be possible to call the same command from C#, although I haven´t tried it yet.

 

 

Best regards, Jack.

 

https://geomarker.wordpress.com/

 

 

 

0 Likes
Message 4 of 6

Anonymous
Not applicable

Just to let you know, you can use "sendstringtoexecute" command and work on that, it is like typing the command line.

Message 5 of 6

t_jevdjic
Advocate
Advocate

Could I insert multiple images (with tfw file) this way without dialog box?

It asks to check checkedlistbox.

 

I do not know how to make reversible process, when I have tfw and tif, because as I understood there should be some calculation about projection of the image. So I concluded that easiest way in my case, is to use mapiinsert. I have to choose amongst 1000 tif files particular files, and when I found these,  I do not know how to do the opposite math.. I'm not geodetic specialist, but I suppose that it could be done with some math.

 

I would appreciate some help regarding this issue...

best,

 

0 Likes
Message 6 of 6

t_jevdjic
Advocate
Advocate

Hi all,

I solved this issue in C# using this tutorial and mapiinsert command.

No need for scr file..

It worked for me.

Besides that I appreciated Gile's posts, so it helped me to solve this issue.

Now,

I would like to do that via fdo.

It requires some learning about that.

here is try part from my button on click:

try
{
await docManager.ExecuteInCommandContextAsync(
async (obj) =>
{
await ed.CommandAsync( "FILEDIA", "0" );
await ed.CommandAsync( "CMDECHO", "0" );
foreach (var raster in rasters){
// MessageBox.Show(raster);
await ed.CommandAsync( "mapiinsert", raster, "N" );
}
await ed.CommandAsync( "FILEDIA", "1" );
await ed.CommandAsync( "CMDECHO", "1" );
await ed.CommandAsync( "ZOOM", "E" );
},
null
);
this.UncheckAllItems();
}

0 Likes