예제 #1
0
        private void SubVarianLoadRaster()
        {
            String PIC_NAME = "C:\\_Temp_\\5525.TIF";

            AcDb.Database CurDb = AcAp.Application.DocumentManager.MdiActiveDocument.Database;


            using (AcDb.Transaction acTrans = CurDb.TransactionManager.StartTransaction())
            {
                try
                {
                    AcDb.ObjectId dictId = AcDb.RasterImageDef.GetImageDictionary(CurDb);
                    if (dictId == null)
                    {
                        dictId = AcDb.RasterImageDef.CreateImageDictionary(CurDb);
                    }

                    AcDb.DBDictionary   dict    = acTrans.GetObject(dictId, AcDb.OpenMode.ForRead) as AcDb.DBDictionary;
                    String              recName = "5525";
                    AcDb.RasterImageDef rid     = new AcDb.RasterImageDef
                    {
                        SourceFileName = PIC_NAME
                    };
                    dict.UpgradeOpen();
                    AcDb.ObjectId defId = dict.SetAt(recName, rid);

                    rid.Load();

                    acTrans.AddNewlyCreatedDBObject(rid, true);
                    AcDb.RasterImage ri = new AcDb.RasterImage
                    {
                        ImageDefId  = defId,
                        ShowImage   = true,
                        Orientation = new AcGe.CoordinateSystem3d(new AcGe.Point3d(200, 300, 0), new AcGe.Vector3d(1, 0, 0), new AcGe.Vector3d(0, 1, 0))
                    };
                    AcDb.BlockTable       bt  = acTrans.GetObject(CurDb.BlockTableId, AcDb.OpenMode.ForRead, false) as AcDb.BlockTable;
                    AcDb.BlockTableRecord btr = acTrans.GetObject(bt[AcDb.BlockTableRecord.ModelSpace], AcDb.OpenMode.ForWrite, false) as AcDb.BlockTableRecord;
                    btr.AppendEntity(ri);
                    acTrans.AddNewlyCreatedDBObject(ri, true);
                    ri.AssociateRasterDef(rid);
                    ri.TransformBy(AcGe.Matrix3d.Scaling(1000, new AcGe.Point3d(200, 300, 0)));
                }
                catch
                {
                    return;
                }
            }

            /*
             *     Try
             *              Dim PIC_NAME As String = original_picture_name
             *              Dim dictId As ObjectId = RasterImageDef.GetImageDictionary(Db)
             *              If dictId = Nothing Then
             *                  dictId = RasterImageDef.CreateImageDictionary(Db)
             *              End If
             *              Dim dict As DBDictionary = CType(acTrans.GetObject(dictId, OpenMode.ForRead), DBDictionary)
             *              Dim recName As String = Picture_name_in_drawing
             *              Dim rid As RasterImageDef = New RasterImageDef
             *              rid.SourceFileName = PIC_NAME
             *              dict.UpgradeOpen()
             *              Dim defId As ObjectId = dict.SetAt(recName, rid)
             *              rid.Load()
             *              acTrans.AddNewlyCreatedDBObject(rid, True)
             *              Dim ri As RasterImage = New RasterImage
             *              ri.ImageDefId = defId
             *              ri.ShowImage = True
             *              ri.Orientation = New CoordinateSystem3d(New Point3d(x1, y1, 0), New Vector3d(1, 0, 0), New Vector3d(0, 1, 0))
             *              Dim bt As BlockTable
             *              Dim btr As BlockTableRecord
             *              bt = acTrans.GetObject(Db.BlockTableId, OpenMode.ForRead, False)
             *              btr = acTrans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, False)
             *              btr.AppendEntity(ri)
             *              acTrans.AddNewlyCreatedDBObject(ri, True)
             *              ri.AssociateRasterDef(rid)
             *              ri.TransformBy(Matrix3d.Scaling(picture_scale, New Point3d(x1, y1, 0)))
             *       Catch
             *
             *              Exit Sub
             *       End Try
             *
             */
        }
예제 #2
0
        public void AttachRasterImage()
        {
            // Get the current database and start a transaction
            AcDb.Database acCurDb;
            acCurDb = AcAp.Application.DocumentManager.MdiActiveDocument.Database;

            using (AcDb.Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // Define the name and image to use
                string strImgName  = "5525";
                string strFileName = "C:\\_Temp_\\5525.TIF";

                AcDb.RasterImageDef acRasterDef;
                bool          bRasterDefCreated = false;
                AcDb.ObjectId acImgDefId;

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

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

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

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

                    acRasterDef = acTrans.GetObject(acImgDefId, AcDb.OpenMode.ForWrite) as AcDb.RasterImageDef;
                }
                else
                {
                    // Create a raster image definition
                    AcDb.RasterImageDef acRasterDefNew = new AcDb.RasterImageDef
                    {
                        // Set the source for the image file
                        SourceFileName = strFileName
                    };

                    acImgDict.UpgradeOpen();
                    acImgDefId = acImgDict.SetAt(strImgName, acRasterDefNew);

                    // Load the image into memory
                    acRasterDefNew.Load();


                    // 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
                AcDb.BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, AcDb.OpenMode.ForRead) as AcDb.BlockTable;

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


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

                    // 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 width and height of the image
                    AcGe.Vector3d width;
                    AcGe.Vector3d height;

                    // Check to see if the measurement is set to English (Imperial) or Metric units
                    if (acCurDb.Measurement == AcDb.MeasurementValue.English)
                    {
                        width  = new AcGe.Vector3d((acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth) / 25.4, 0, 0);
                        height = new AcGe.Vector3d(0, (acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight) / 25.4, 0);
                    }
                    else
                    {
                        width  = new AcGe.Vector3d(acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth, 0, 0);
                        height = new AcGe.Vector3d(0, acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight, 0);
                    }

                    // Define the position for the image
                    AcGe.Point3d insPt = new AcGe.Point3d(52000.0, 56000.0, 100.0);

                    // Define and assign a coordinate system for the image's orientation
                    AcGe.CoordinateSystem3d coordinateSystem = new AcGe.CoordinateSystem3d(insPt, width * 2, height * 2);
                    acRaster.Orientation = coordinateSystem;

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

                    // 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.
                    /*AcDb.RasterImage.EnableReactors(true);*/
                    acRaster.AssociateRasterDef(acRasterDef);
                    acRaster.ShowImage         = true;
                    acRaster.ColorIndex        = 200;
                    acRaster.ImageTransparency = true;
                    acRaster.Orientation       = new AcGe.CoordinateSystem3d(insPt, new AcGe.Vector3d(1000, 0, 0), new AcGe.Vector3d(0, 2000, 0));

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

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

                // Dispose of the transaction
            }
        }