.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

.Net c# updating xref path without opening the drawing.

4 REPLIES 4
Reply
Message 1 of 5
raghulan
4148 Views, 4 Replies

.Net c# updating xref path without opening the drawing.

Hello coders, 

 

 I am building a plugin which needs to change the xref path within a drawing without opening it.

 

here is my. please throw some light on where i went wrong..I am a newbiee..to .Net c#

 

=====================================================================

 

[CommandMethod("refremap")]
public void refremap() // This method can have any name
{
//Get the document
Document Doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = Doc.Editor;

// Ask the user to select a folder

PromptResult res = ed.GetString("\nEnter the path of the folder: ");

if (res.Status == PromptStatus.OK)
{
string[] filepaths = Directory.GetFiles(res.StringResult, "*.dwg", SearchOption.AllDirectories);
int filecount = filepaths.Length;
ed.WriteMessage("\nScanning " + filecount + " files");
int finalxrefcount = 0;
int xrefedfile = 0;


for (int i = 0; i < filecount; i++)
{

ed.WriteMessage("\n File Name : " + filepaths[i]);
//create a database and try to load the file
Database db = new Database(false, true);
using (db)
{
try
{
db.ReadDwgFile(filepaths[i], System.IO.FileShare.Read, false, "");
}
catch (System.Exception)
{
ed.WriteMessage("\nUnable to read the drawingfile.");
return;
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ed.WriteMessage("\n--------Xrefs Details--------");
db.ResolveXrefs(true, false);

XrefGraph xg = db.GetHostDwgXrefGraph(true);
GraphNode root = xg.RootNode;
//XrefObjectId xobj = new XrefObjectId();
ObjectIdCollection objcoll = new ObjectIdCollection();

int xrefcount = xg.NumNodes - 1;

if (xrefcount == 0)
{
ed.WriteMessage("\nNo xrefs found in the drawing");

}
else
{
for (int r = 1; r < (xrefcount+1); r++)
{
XrefGraphNode child = xg.GetXrefNode(r);
if (child.XrefStatus == XrefStatus.FileNotFound)
{

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(child.BlockTableRecordId, OpenMode.ForWrite);

db.XrefEditEnabled = true;

string originalpath = btr.PathName;
string childname = Path.GetFileName(originalpath);
string newpath = @"E:\acad_dev_testing22\test\" + childname;

btr.PathName = newpath;


ed.WriteMessage("\n xref old path: " + originalpath);
ed.WriteMessage("\n xref new path: " + newpath + " xref fixed !!");


}

}

}

tr.Commit();
}

}

}
ed.WriteMessage("\nTotal number of files scanned: " + filecount);
ed.WriteMessage("\nTotal number of files that contains xref : " + xrefedfile);
ed.WriteMessage("\nTotal number of xrefs : " + finalxrefcount);
ed.WriteMessage("\nTotal number of xrefs attached and working : " + filecount);
ed.WriteMessage("\nTotal number of xrefs that are not working : " + filecount);
ed.WriteMessage("\n----------------------------------------\n");
ed.WriteMessage("\n----------------------------------------\n");
}
else
{
return;
}
}

Regards,

Raghulan Gowthaman Cert IV TAA, B.E,.
Senior Technical Consultant | Developer - R&D
A2K Technologies Sydney
Web : www.a2ktechnologies.com.au
www.civil3dforum.com | www.e4forums.com
www.zcodia.com.au
www.raghulangowthaman.com
4 REPLIES 4
Message 2 of 5
khoa.ho
in reply to: raghulan

We can fix the following code (in red):

 

using (db)
{
    try
    {
        db.ReadDwgFile(filepaths[i], FileShare.Read, false, "");
    }
    catch (System.Exception)
    {
        ed.WriteMessage("\nUnable to read the drawingfile.");
        return;
    }
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        ed.WriteMessage("\n--------Xrefs Details--------");
        db.ResolveXrefs(true, false);

        XrefGraph xg = db.GetHostDwgXrefGraph(true);
        int xrefcount = xg.NumNodes - 1;

        if (xrefcount == 0)
        {
            ed.WriteMessage("\nNo xrefs found in the drawing");
        }
        else
        {
            for (int r = 1; r < (xrefcount + 1); r++)
            {
                XrefGraphNode child = xg.GetXrefNode(r);
                if (child.XrefStatus == XrefStatus.Resolved)
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(child.BlockTableRecordId, OpenMode.ForWrite);

                    db.XrefEditEnabled = true;

                    string originalpath = btr.PathName;
                    string childname = Path.GetFileName(originalpath);
                    string newpath = @"E:\acad_dev_testing22\test\" + childname;
                    btr.PathName = newpath;

                    ed.WriteMessage("\n xref old path: " + originalpath);
                    ed.WriteMessage("\n xref new path: " + newpath + " xref fixed !!");
                }
            }
            tr.Commit();
        }
    }

    // Overwrite the current drawing file with new updated XRef paths
    db.SaveAs(filepaths[i], DwgVersion.Current);
}

 

You should save the database back to its file after making changes. Don't need to commit transaction if this file does not have xrefs.

 

-Khoa

Message 3 of 5
raghulan
in reply to: khoa.ho

Thanks mate for you advice,

I am getting following error:

Autodesk.AutoCAD.Runtime.Exception: eFileSharingViolation
at Autodesk.AutoCAD.DatabaseServices.Database.SaveAs(String fileName, DwgVersion version)
at Xref.MyCommands.refremap() in D:\Dev Zone\Ms.NET\Projects\Xref\Xref\myCommands.cs:line 361
at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)

========================================================================================

 

XrefGraph xg = db.GetHostDwgXrefGraph(true);
GraphNode root = xg.RootNode;
//XrefObjectId xobj = new XrefObjectId();
ObjectIdCollection objcoll = new ObjectIdCollection();

int xrefcount = xg.NumNodes - 1;

if (xrefcount == 0)
{
ed.WriteMessage("\nNo xrefs found in the drawing");

}
else
{
for (int r = 1; r < (xrefcount + 1); r++)
{
XrefGraphNode child = xg.GetXrefNode(r);
if (child.XrefStatus == XrefStatus.Resolved)
{

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(child.BlockTableRecordId, OpenMode.ForWrite);

db.XrefEditEnabled = true;

string originalpath = btr.PathName;
string childname = Path.GetFileName(originalpath);
string newpath = @"D:\test_area\xref\sub_folder\" + childname;

btr.PathName = newpath;

ed.WriteMessage("\n xref old path: " + originalpath);
ed.WriteMessage("\n xref new path: " + newpath + " xref fixed !!");


}


}
tr.Commit();
}

}
db.SaveAs(filepaths[i], DwgVersion.Current);
}

Regards,

Raghulan Gowthaman Cert IV TAA, B.E,.
Senior Technical Consultant | Developer - R&D
A2K Technologies Sydney
Web : www.a2ktechnologies.com.au
www.civil3dforum.com | www.e4forums.com
www.zcodia.com.au
www.raghulangowthaman.com
Message 4 of 5
Balaji_Ram
in reply to: raghulan

Try using "System.IO.FileShare.ReadWrite" as the parameter in "ReadDwgFile".



Balaji
Developer Technical Services
Autodesk Developer Network

Message 5 of 5
raghulan
in reply to: Balaji_Ram

Thanks Balaji, Will try and update

Regards,

Raghulan Gowthaman Cert IV TAA, B.E,.
Senior Technical Consultant | Developer - R&D
A2K Technologies Sydney
Web : www.a2ktechnologies.com.au
www.civil3dforum.com | www.e4forums.com
www.zcodia.com.au
www.raghulangowthaman.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost