• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Active Contributor
    Posts: 47
    Registered: ‎12-15-2009

    autocad overwrite raster image file while host drawing is open

    149 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

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    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
    Please use plain text.
    Active Contributor
    Posts: 47
    Registered: ‎12-15-2009

    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

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    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
    Please use plain text.
    Active Contributor
    Posts: 47
    Registered: ‎12-15-2009

    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.

    Please use plain text.
    Active Contributor
    Posts: 47
    Registered: ‎12-15-2009

    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.

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    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
    Please use plain text.