Wrong justification of DBText in block

Wrong justification of DBText in block

lotuslakeU5KLM
Participant Participant
980 Views
5 Replies
Message 1 of 6

Wrong justification of DBText in block

lotuslakeU5KLM
Participant
Participant

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.

01 org.png

02 final.png

03 bedit.png

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();
            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;

                        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];
                        if (dBTexts.Count == 3)
                            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));
                        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;
                        }
                        #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图框(新)"
                                           };
                        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];
                                            }
                                        }
                                        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());
            }
        }
0 Likes
Accepted solutions (1)
981 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant

Hi,

 

You should have a look at the DBText.AdjustAlignment method.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 6

norman.yuan
Mentor
Mentor
Accepted solution

This is a "WELL"-known issue: if you add Text/AttributeReference of block in a side database, you need to set the side database as WorkingDatabase, or the Text/Attribute would not align correctly. See these previous discussions:

 

https://forums.autodesk.com/t5/net/changed-attribute-text-in-sidedb-does-not-re-align-when/m-p/86650... 

https://forums.autodesk.com/t5/net/attrubute-text-alignment-problem-after-update/m-p/2624675#M17532 

 

By the way, your code SHOULD dispose the side database after done with it (especially when your code is doing multiple side databases), that is:

var originalDb = HostApplicationService.WorkingDatabase;

for each (....)

{

  using ( var acCurDb = new Database(false, true))

  {

     // read from dwg file

    HostApplicationServices.WorkingDatabase = acCurDb;

     // process the side data here

  }

}

HostApplicationServices.WorkingDatabase = originalDb;

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 4 of 6

lotuslakeU5KLM
Participant
Participant
Thanks a lot. I changed the code as following: switch (comboBoxType.Text) { case "工程名": ProjectName.AdjustAlignment(acCurDb); ProjectName.TextString = textBoxValue.Text; ProjectName.AdjustAlignment(acCurDb); ... Unfortunately, it doesn't works.
0 Likes
Message 5 of 6

lotuslakeU5KLM
Participant
Participant

Thanks for your kind information. That's what I want.

0 Likes
Message 6 of 6

a932994040
Participant
Participant

hello,following your example,the code is modified but the position is not fixed,can you help me

0 Likes