.NET

Reply
Valued Contributor
mnav
Posts: 58
Registered: ‎12-15-2009
Message 1 of 7 (283 Views)

autocad overwrite raster image file while host drawing is open

283 Views, 6 Replies
08-09-2012 01:54 AM

Hi

 i am trying to overwrite the raster image file to an updated version of same file. when i try to copy file it says file is in use by another process IOException.

i can change the path to new loaction and reload image but cant overwite the file at same location.

 

is there any way to overwite the raster image file while the host drawing is open.

 

i am using c# and autocad 2010

 

thanks

Nav

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 7 (280 Views)

Re: autocad overwrite raster image file while host drawing is open

08-09-2012 02:09 AM in reply to: mnav

Have a look at this article:

 

http://adndevblog.typepad.com/autocad/2012/07/getset-image-paths-in-drawing-using-net-api.html

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
mnav
Posts: 58
Registered: ‎12-15-2009
Message 3 of 7 (279 Views)

Re: autocad overwrite raster image file while host drawing is open

08-09-2012 02:21 AM in reply to: Hallex

thanks for your reply.

 

i can change the location of  image file without any problem. Error IoException comes if i try to overwite the image file which already in used in open drawing.

 

i am looking for a way to overwite image file like we can do in drawing Xrefs we can change the source file even though its open in drawing. Autocad just pop a message that an updated version of xref is avialable. but in raster image case its not allowing to update file.

 

thanks

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 7 (273 Views)

Re: autocad overwrite raster image file while host drawing is open

08-09-2012 02:31 AM in reply to: mnav

If you see new image as available, try add this code block

to know if it's non-unreferenced:

 

        RasterImage.EnableReactors(true);

 

        myraster.AssociateRasterDef(imageId);

 

       tr.commit();

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
mnav
Posts: 58
Registered: ‎12-15-2009
Message 5 of 7 (269 Views)

Re: autocad overwrite raster image file while host drawing is open

08-09-2012 02:49 AM in reply to: Hallex

thanks i will give it a go. but problem comes before setting any flags when overwiting file.

Valued Contributor
mnav
Posts: 58
Registered: ‎12-15-2009
Message 6 of 7 (266 Views)

Re: autocad overwrite raster image file while host drawing is open

08-09-2012 03:05 AM in reply to: mnav

 

for eample if Rasterimage location is C:\MyFiles\image1.bmp and if i download the image1.bmp again on same location i got IOException saying file is already in use. file access error.

 

i am downloading the file programatically on update image button.

if file dont exist already my code works fine download the file and load it into the drawing. problem comes if file alredy exist and refrenced into drawing.

 

hope this will explain little bit more.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 7 of 7 (244 Views)

Re: autocad overwrite raster image file while host drawing is open

08-09-2012 10:37 AM in reply to: mnav

See if this code is working for you, tested on A2010 especially

 

       //  original code:
        //  http://adndevblog.typepad.com/autocad/2012/07/getset-image-paths-in-drawing-using-net-api.html
        [CommandMethod("imagePath")]

        public void EditImagePath()
        {
            Database db = HostApplicationServices.WorkingDatabase;

            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

            PromptEntityOptions peo = new PromptEntityOptions("\nSelect an Image");

            peo.SetRejectMessage("\nSelected entity must be an image.");

            peo.AddAllowedClass(typeof(RasterImage), false);

            PromptEntityResult per = ed.GetEntity(peo);

            if (per.Status != PromptStatus.OK)

                return;

            ObjectId imageId = per.ObjectId;

            try
            {

                using (Transaction trans = db.TransactionManager.StartTransaction())
                {

                    RasterImage myImage = trans.GetObject(imageId, OpenMode.ForRead) as RasterImage;

                    RasterImageDef imageDef = trans.GetObject(myImage.ImageDefId, OpenMode.ForWrite) as RasterImageDef;

                    ed.WriteMessage(imageDef.ActiveFileName);

                    imageDef.SourceFileName = @"C:\Test\jela.bmp";

                    imageDef.ActiveFileName = @"C:\Test\jela.bmp";
                   
                    myImage.UpgradeOpen();
                   
                    myImage.Name = "jela";
                    
                    //imageDef.LocateActivePath();// optional, has no affect in this context

                    imageDef.Load();

                    RasterImage.EnableReactors(true);

                    myImage.AssociateRasterDef(imageDef);
                   
                    trans.Commit();
                   
                }

            }

            catch (System.Exception ex)
            {

                ed.WriteMessage(ex.Message);

            }

        }

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!