public void CT_ROADS_UpdateTargets_Refs()
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
CivilDocument civilDoc = CivilApplication.ActiveDocument;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database acCurDb = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
DocumentCollection dc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
//picking dxf
Autodesk
.AutoCAD.Windows.OpenFileDialog ofdUpdate
= new Autodesk
.AutoCAD.Windows.OpenFileDialog("Select DXF with new data",
"",
"",
"DXF", Autodesk
.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple); ofdUpdate.ShowDialog();
string[] UpdateFilePaths = ofdUpdate.GetFilenames();
if (( UpdateFilePaths != null ))
for (int i = 0; i < UpdateFilePaths.Length; i++)
{
UpdateFilePaths[i] = UpdateFilePaths[i].ToUpper();
}
ObjectIdCollection DonePolysIds
= new ObjectIdCollection
();
if (( UpdateFilePaths != null ))
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
//Getting xrefs from current file
string[] XRefNames
= new string[100]; ObjectId
[] XRefIds
= new ObjectId
[100]; BlockTable OrigBT = (BlockTable)acCurDb.BlockTableId.GetObject(OpenMode.ForWrite);
int i = 0;
foreach (ObjectId OrigBtrId in OrigBT)
{
BlockTableRecord OrigBtr = (BlockTableRecord)OrigBtrId.GetObject(OpenMode.ForRead);
foreach (ObjectId TMPId in OrigBtr)
{
if (TMPId.ObjectClass.DxfName == "INSERT")
{
BlockReference ERTMP = (BlockReference)TMPId.GetObject(OpenMode.ForRead);
BlockTableRecord ERBtrTMP = (BlockTableRecord)ERTMP.BlockTableRecord.GetObject(OpenMode.ForRead);
if (ERBtrTMP.IsFromExternalReference)
{
XRefIds[i] = TMPId;
XRefNames[i] = ERTMP.Name;
i++;
}
}
}
}
CorridorCollection CorCol = civilDoc.CorridorCollection;
foreach (ObjectId CorId in CorCol)
{
Corridor Cor = (Corridor)CorId.GetObject(OpenMode.ForWrite);
foreach (Baseline Bl in Cor.Baselines)
{
Alignment alg = (Alignment)Bl.AlignmentId.GetObject(OpenMode.ForRead);
foreach (BaselineRegion Blr in Bl.BaselineRegions)
{
//these points will be needed to determine
//the side of polyline relative to baseline
Point2d AlAP = P3dToP2d(alg.GetPointAtDist(Blr.StartStation));
Point2d AlBP = P3dToP2d(alg.GetPointAtDist(Blr.StartStation + 1));
SubassemblyTargetInfoCollection stic = Blr.GetTargets();
foreach (SubassemblyTargetInfo sti in stic)
{
foreach (ObjectId TId in sti.TargetIds)
{
if (( TId.ObjectClass.DxfName == "LWPOLYLINE" ) && ( TId.Database.Filename.Contains(".dwg") ) && ( DonePolysIds.Contains(TId) == false ))
{
string XRefFileName = ( Regex.Match(TId.Database.Filename, "(?<=\\\\(?!.*\\\\))(.*)", RegexOptions.None) ).Value;
XRefFileName = XRefFileName.Remove(XRefFileName.IndexOf(".dwg"));
string XRefPath = ( TId.Database.Filename.Remove(TId.Database.Filename.IndexOf(".dwg")) + ".dxf" ).ToUpper();
//if found poly are from ref file (TId.Database.Filename!="")
//and its file name are in list of picked DXFs - updating starts
if (UpdateFilePaths.Contains(XRefPath))
{
if (IsFileLockedOrReadOnly
(new FileInfo
(XRefPath
))) {
ed.WriteMessage("\nUnable to read XREF: " + XRefPath);
return;
}
else
{
//getting DB of XREF
BlockReference XRefBlk = (BlockReference)XRefIds[Array.IndexOf(XRefNames, XRefFileName)].GetObject(OpenMode.ForWrite);
BlockTableRecord XRefBlkTR = (BlockTableRecord)XRefBlk.BlockTableRecord.GetObject(OpenMode.ForWrite);
Database XRefDb = XRefBlkTR.GetXrefDatabase(false);
using (var xf = XrefFileLock.LockFile(XRefDb.XrefBlockId))
{
XRefDb.RestoreOriginalXrefSymbols();
BlockTable XRefBT = (BlockTable)XRefDb.BlockTableId.GetObject(OpenMode.ForWrite);
BlockTableRecord XRefMS = (BlockTableRecord)XRefBT[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite);
ObjectId XRefPolyId
= new ObjectId
();
//Finding poly to update
foreach (ObjectId UpdateBtrId in XRefMS)
{
if (UpdateBtrId.ToString() == TId.ToString())
XRefPolyId = UpdateBtrId;
}
Autodesk.AutoCAD.DatabaseServices.Polyline XRefPoly = (Autodesk.AutoCAD.DatabaseServices.Polyline)XRefPolyId.GetObject(OpenMode.ForWrite);
//determine polyline side
Point2d XRefPolyP = P3dToP2d(XRefPoly.GetClosestPointTo(alg.GetPointAtDist(Blr.StartStation), true));
double XRefPolySide = LeftOrRight(AlAP, AlBP, XRefPolyP);
//Getting poly info from DXF
//as DXF is a text file - read it as text file
string ln;
bool huyl = false;
bool plin = false;
int UpdPlinesCounter = 0;
string PrevStr = "";
string PolyLayer = "";
int verC = 0;
double PolyX = 0;
int cntr = 0;
Autodesk
.AutoCAD.DatabaseServices.Polyline Poly
= new Autodesk
.AutoCAD.DatabaseServices.Polyline(); using (StreamReader DXFRem
= new StreamReader
(XRefPath
)) {
while (( ln = DXFRem.ReadLine() ) != null)
{
cntr++;
//here is a logic to find a needed poly from all polys inside DXF
//It first creates a "Poly" from DXF data.
//Then it compares side and layer of found poly and xref poly
//if found poly on same side and layer - updating.
if (ln == "AcDbEntity")
{
huyl = true;
plin = false;
i++;
PrevStr = "";
verC = 0;
if (Poly.NumberOfVertices > 2)
{
Point2d UpdatePolyP = P3dToP2d(Poly.GetClosestPointTo(alg.GetPointAtDist(Blr.StartStation), true));
double UpdatePolySide = LeftOrRight(AlAP, AlBP, UpdatePolyP);
if (PolyLayer == XRefPoly.Layer && XRefPolySide == UpdatePolySide)
{
Point3dCollection PolyPnts
= new Point3dCollection
(); Poly.GetStretchPoints(PolyPnts);
for (int DelP = XRefPoly.NumberOfVertices - 1; DelP > 0; DelP--)
{
XRefPoly.RemoveVertexAt(DelP);
}
for (int InsP = 1; InsP < PolyPnts.Count; InsP++)
{
XRefPoly.AddVertexAt(InsP, P3dToP2d(PolyPnts[InsP]), 0, 0, 0);
}
XRefPoly.RemoveVertexAt(0);
XRefPoly.ColorIndex = 10;
break;
}
}
PolyLayer = "";
Poly
= new Autodesk
.AutoCAD.DatabaseServices.Polyline(); }
if (ln == "AcDbPolyline")
{
plin = true;
}
if (huyl)
{
switch (PrevStr)
{
case " 8":
PolyLayer = ln;
break;
case " 10":
if (plin) PolyX = Convert.ToDouble(ln);
break;
case " 20":
if (plin)
{
Poly
.AddVertexAt(verC,
new Point2d
(PolyX, Convert
.ToDouble(ln
)),
0,
0,
0); verC++;
}
break;
}
PrevStr = ln;
}
}
}
DonePolysIds.Add(TId);
XRefDb.RestoreForwardingXrefSymbols();
}
}
}
}
}
}
}
}
break;
}
acTrans.Commit();
}
}
public static bool IsFileLockedOrReadOnly(FileInfo fi)
{
FileStream fs = null;
try
{
fs =
fi.Open(
FileMode.Open, FileAccess.ReadWrite, FileShare.None
);
}
catch (System.Exception ex)
{
if (ex
is IOException
|| ex
is UnauthorizedAccessException
) {
return true;
}
throw;
}
finally
{
if (fs != null)
fs.Close();
}
// File is accessible
return false;
}
public static int LeftOrRight(Point2d A, Point2d B, Point2d P)
{
if (( B.X - A.X ) * ( P.Y - A.Y ) - ( B.Y - A.Y ) * ( P.X - A.X ) > 0)
return 1;
else
return -1;
}