.NET

Reply
Valued Contributor
raghulan
Posts: 92
Registered: ‎01-11-2012
Message 1 of 5 (1,043 Views)

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

1043 Views, 4 Replies
08-06-2012 05:17 AM

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
Mentor
khoa.ho
Posts: 214
Registered: ‎09-15-2011
Message 2 of 5 (1,028 Views)

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

08-06-2012 12:26 PM 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

Valued Contributor
raghulan
Posts: 92
Registered: ‎01-11-2012
Message 3 of 5 (986 Views)

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

08-08-2012 11:50 PM 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
ADN Support Specialist
Balaji_Ram
Posts: 701
Registered: ‎03-21-2011
Message 4 of 5 (968 Views)

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

08-10-2012 07:02 AM in reply to: raghulan

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



Balaji
Developer Technical Services
Autodesk Developer Network

Valued Contributor
raghulan
Posts: 92
Registered: ‎01-11-2012
Message 5 of 5 (945 Views)

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

08-13-2012 01:42 AM 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
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.