Message 1 of 6
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Dear Sir/Madam, I’m reading and writing a .dwg file by the method of DataBase.ReadDwgFile() in purpose of change the content of some blocks. To my disappointment, I found that the DBTexts modified in the blocks got the wrong position (looks like due to wrong text justification). However, when I open the dwg files in AutoCAD and perform BEDIT command, the text position and justification become perfect at once. At the same time, BSAVE must be performed to keep the right text position. If the BEDIT process is canceled without saving, the DBTexts get the wrong position again. Grateful for any response.
private void buttonChange_Click(object sender, EventArgs e)
{
Database acCurDb = new Database(false, true);
DBText DrawingTitle = new DBText(), DrawingSn = new DBText(), ProjectName = new DBText(),
RegionAndUnitName = new DBText(), StructureName = new DBText(), StructureId = new DBText();
DBText ProjectId = new DBText(), Version = new DBText(), Stage = new DBText(), Date = new DBText();
DBText HiddenStructureId = new DBText();
{
Database acCurDb = new Database(false, true);
DBText DrawingTitle = new DBText(), DrawingSn = new DBText(), ProjectName = new DBText(),
RegionAndUnitName = new DBText(), StructureName = new DBText(), StructureId = new DBText();
DBText ProjectId = new DBText(), Version = new DBText(), Stage = new DBText(), Date = new DBText();
DBText HiddenStructureId = new DBText();
try
{
foreach (DataGridViewRow r in this.dataGridView1.Rows)
{
string filename = r.Cells[1].Value as string;
acCurDb = new Database(false, true);
if (File.Exists(filename.Replace(".dwg", ".dwg_")))
File.Delete(filename.Replace(".dwg", ".dwg_"));
File.Move(filename, filename.Replace(".dwg", ".dwg_"));
acCurDb.ReadDwgFile(filename + "_", FileOpenMode.OpenForReadAndAllShare, false, "");
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
#region 提取项目信息
ObjectId blkRecId;
BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
try
{
blkRecId = acBlkTbl["_CPPEtj角图章"];
}
catch
{
blkRecId = acBlkTbl["_CPPETJ角图章(旧)"];
}
BlockTableRecord acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
{
foreach (DataGridViewRow r in this.dataGridView1.Rows)
{
string filename = r.Cells[1].Value as string;
acCurDb = new Database(false, true);
if (File.Exists(filename.Replace(".dwg", ".dwg_")))
File.Delete(filename.Replace(".dwg", ".dwg_"));
File.Move(filename, filename.Replace(".dwg", ".dwg_"));
acCurDb.ReadDwgFile(filename + "_", FileOpenMode.OpenForReadAndAllShare, false, "");
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
#region 提取项目信息
ObjectId blkRecId;
BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
try
{
blkRecId = acBlkTbl["_CPPEtj角图章"];
}
catch
{
blkRecId = acBlkTbl["_CPPETJ角图章(旧)"];
}
BlockTableRecord acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
List<DBText> dBTexts = new List<DBText>();
List<DBText> dBTexts2 = new List<DBText>();
foreach (ObjectId objID in acBlkTblRec)
{
if (objID.GetObject(OpenMode.ForRead).GetRXClass().DxfName == "TEXT")
{
DBText Dt = new DBText();
Dt = objID.GetObject(OpenMode.ForWrite) as DBText;
if (Dt.Height > 4.9 && Dt.Height < 5.1 && Dt.WidthFactor < 0.9 && Dt.Layer == "A角图章1")
dBTexts.Add(Dt);
else if (Dt.Height > 3.5 && Dt.Height < 3.7 && Dt.Layer == "A角图章1")
dBTexts2.Add(Dt);
else if (Dt.Height > 2.9 && Dt.Height < 3.1 && Dt.Layer == "A角图章说明1" && !Dt.TextString.Contains("档案号"))
{
if (comboBoxType.Text == "单体号")
Dt.TextString = comboBoxType.Text;
StructureId = Dt;
}
}
}
dBTexts.Sort((a, b) => b.Position.Y.CompareTo(a.Position.Y)); // ♥♥♥♥♥ 值得收藏的排序语句 ♥♥♥
ProjectName = dBTexts[0];
StructureName = dBTexts[dBTexts.Count - 1];
dBTexts.Add(Dt);
else if (Dt.Height > 3.5 && Dt.Height < 3.7 && Dt.Layer == "A角图章1")
dBTexts2.Add(Dt);
else if (Dt.Height > 2.9 && Dt.Height < 3.1 && Dt.Layer == "A角图章说明1" && !Dt.TextString.Contains("档案号"))
{
if (comboBoxType.Text == "单体号")
Dt.TextString = comboBoxType.Text;
StructureId = Dt;
}
}
}
dBTexts.Sort((a, b) => b.Position.Y.CompareTo(a.Position.Y)); // ♥♥♥♥♥ 值得收藏的排序语句 ♥♥♥
ProjectName = dBTexts[0];
StructureName = dBTexts[dBTexts.Count - 1];
if (dBTexts.Count == 3)
RegionAndUnitName = dBTexts[1];
RegionAndUnitName = dBTexts[1];
ProjectId = LotusTools.dbTextBetweenPoints(dBTexts2, new Point2d(-50, 12), new Point2d(0, 6));
Stage = LotusTools.dbTextBetweenPoints(dBTexts2, new Point2d(-90, 18), new Point2d(-68, 12));
Version = LotusTools.dbTextBetweenPoints(dBTexts2, new Point2d(-50, 6), new Point2d(0, 0));
if (Date.TextString == "Undefined" || Date.TextString == "")
Date = LotusTools.dbTextBetweenPoints(dBTexts2, new Point2d(-90, 6), new Point2d(-68, 0));
Stage = LotusTools.dbTextBetweenPoints(dBTexts2, new Point2d(-90, 18), new Point2d(-68, 12));
Version = LotusTools.dbTextBetweenPoints(dBTexts2, new Point2d(-50, 6), new Point2d(0, 0));
if (Date.TextString == "Undefined" || Date.TextString == "")
Date = LotusTools.dbTextBetweenPoints(dBTexts2, new Point2d(-90, 6), new Point2d(-68, 0));
switch (comboBoxType.Text)
{
case "工程名":
ProjectName.TextString = textBoxValue.Text;
break;
case "项目号":
ProjectId.TextString = textBoxValue.Text;
break;
case "区域名":
string[] texts = RegionAndUnitName.TextString.Split('.');
RegionAndUnitName.TextString = textBoxValue.Text;
if (texts.Length > 1)
RegionAndUnitName.TextString += "." + texts[1];
break;
case "单元名":
RegionAndUnitName.TextString = RegionAndUnitName.TextString.Split('.')[0]
+ "." + textBoxValue.Text;
break;
case "单体名":
StructureName.TextString = textBoxValue.Text;
break;
case "阶段":
Stage.TextString = textBoxValue.Text;
break;
case "版次":
Version.TextString = textBoxValue.Text;
break;
}
{
case "工程名":
ProjectName.TextString = textBoxValue.Text;
break;
case "项目号":
ProjectId.TextString = textBoxValue.Text;
break;
case "区域名":
string[] texts = RegionAndUnitName.TextString.Split('.');
RegionAndUnitName.TextString = textBoxValue.Text;
if (texts.Length > 1)
RegionAndUnitName.TextString += "." + texts[1];
break;
case "单元名":
RegionAndUnitName.TextString = RegionAndUnitName.TextString.Split('.')[0]
+ "." + textBoxValue.Text;
break;
case "单体名":
StructureName.TextString = textBoxValue.Text;
break;
case "阶段":
Stage.TextString = textBoxValue.Text;
break;
case "版次":
Version.TextString = textBoxValue.Text;
break;
}
#endregion 提取项目信息
#region 提取图框信息
string[] Frames = { "A0图框", "A0图框(新)", "A0图框(竖)", "A0图框(竖)(新)",
"A1图框", "A1图框(新)", "A1图框(竖)", "A1图框(竖)(新)",
"A0+1/2图框", "A0+1/2图框(新)",
"A0+1/4图框", "A0+1/4图框(新)",
"A1+1/2图框", "A1+1/2图框(新)",
"A1+1/4图框", "A1+1/4图框(新)", "A1+1/4图框(竖)", "A1+1/4图框(竖)(新)",
"A2图框", "A2图框(新)", "A2图框(竖)", "A2图框(竖)(新)",
"A2+1/4图框", "A2+1/4图框(新)",
"A2+1/2图框", "A2+1/2图框(新)", "A2+1/1图框", "A2+1/1图框(新)",
"A3图框", "A3图框(新)",
"A3+1/2图框", "A3+1/2图框(新)",
"A3+1/1图框", "A3+1/1图框(新)"
};
string[] Frames = { "A0图框", "A0图框(新)", "A0图框(竖)", "A0图框(竖)(新)",
"A1图框", "A1图框(新)", "A1图框(竖)", "A1图框(竖)(新)",
"A0+1/2图框", "A0+1/2图框(新)",
"A0+1/4图框", "A0+1/4图框(新)",
"A1+1/2图框", "A1+1/2图框(新)",
"A1+1/4图框", "A1+1/4图框(新)", "A1+1/4图框(竖)", "A1+1/4图框(竖)(新)",
"A2图框", "A2图框(新)", "A2图框(竖)", "A2图框(竖)(新)",
"A2+1/4图框", "A2+1/4图框(新)",
"A2+1/2图框", "A2+1/2图框(新)", "A2+1/1图框", "A2+1/1图框(新)",
"A3图框", "A3图框(新)",
"A3+1/2图框", "A3+1/2图框(新)",
"A3+1/1图框", "A3+1/1图框(新)"
};
BlockTableRecord extModelSpace = (BlockTableRecord)acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead);
foreach (ObjectId id in extModelSpace)
{
if (id.ObjectClass.DxfName == "INSERT")
{
BlockReference br = id.GetObject(OpenMode.ForWrite) as BlockReference;
foreach (string frame in Frames)
if (br.Name == frame)
{
foreach (ObjectId oid in br.AttributeCollection)
{
AttributeReference attref = oid.GetObject(OpenMode.ForWrite) as AttributeReference;
if ((attref.Tag == "日期" && comboBoxType.Text == "日期") ||
(attref.Tag == "比例" && comboBoxType.Text == "比例"))
attref.TextString = textBoxValue.Text;
else if (attref.Tag.Contains("文件号") && comboBoxType.Text == "单体号")
{
string[] texts = attref.TextString.Split('-');
attref.TextString = texts[0] + "-" + StructureId + "-" + texts[2];
}
}
foreach (ObjectId id in extModelSpace)
{
if (id.ObjectClass.DxfName == "INSERT")
{
BlockReference br = id.GetObject(OpenMode.ForWrite) as BlockReference;
foreach (string frame in Frames)
if (br.Name == frame)
{
foreach (ObjectId oid in br.AttributeCollection)
{
AttributeReference attref = oid.GetObject(OpenMode.ForWrite) as AttributeReference;
if ((attref.Tag == "日期" && comboBoxType.Text == "日期") ||
(attref.Tag == "比例" && comboBoxType.Text == "比例"))
attref.TextString = textBoxValue.Text;
else if (attref.Tag.Contains("文件号") && comboBoxType.Text == "单体号")
{
string[] texts = attref.TextString.Split('-');
attref.TextString = texts[0] + "-" + StructureId + "-" + texts[2];
}
}
blkRecId = acBlkTbl[frame];
}
}
}
#endregion 提取图框信息
acTrans.Commit();
}
acCurDb.CloseInput(true);
acCurDb.SaveAs(filename.TrimEnd('_'), DwgVersion.Current);
r.Cells[0].Value = true;
}
}
catch (System.Exception err)
{
System.Windows.Forms.MessageBox.Show(acCurDb.OriginalFileName + "错误:" + err.ToString());
}
}
}
}
}
#endregion 提取图框信息
acTrans.Commit();
}
acCurDb.CloseInput(true);
acCurDb.SaveAs(filename.TrimEnd('_'), DwgVersion.Current);
r.Cells[0].Value = true;
}
}
catch (System.Exception err)
{
System.Windows.Forms.MessageBox.Show(acCurDb.OriginalFileName + "错误:" + err.ToString());
}
}
Solved! Go to Solution.