diff --git a/connor_zwcadm/commands/ConvertPDFCommand.cs b/connor_zwcadm/commands/ConvertPDFCommand.cs index ea95adc..d860534 100644 --- a/connor_zwcadm/commands/ConvertPDFCommand.cs +++ b/connor_zwcadm/commands/ConvertPDFCommand.cs @@ -88,23 +88,23 @@ namespace connor_zwcadm.commands try { KUtil.Log("plotData.TitleBlockName====" + plotData.TitleBlockName); - if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A4及portrait", StringComparison.OrdinalIgnoreCase) >= 0) + if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A4", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("portrait", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A4S.pc5", null); } - else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A4及landscape", StringComparison.OrdinalIgnoreCase) >= 0) + else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A4", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("landscape", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A4H.pc5", null); } - else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A3及landscape", StringComparison.OrdinalIgnoreCase) >= 0) + else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A3", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("landscape", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A3H.pc5", null); } - else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A2及landscape", StringComparison.OrdinalIgnoreCase) >= 0) + else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A2", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("landscape", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A2H.pc5", null); } - else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A1及landscape", StringComparison.OrdinalIgnoreCase) >= 0) + else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A1", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("landscape", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A1H.pc5", null); } @@ -127,23 +127,23 @@ namespace connor_zwcadm.commands KUtil.Log("1111111111111111111"); - if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A4及portrait", StringComparison.OrdinalIgnoreCase) >= 0) + if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A4", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("portrait", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A4S.pc5", null); } - else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A4及landscape", StringComparison.OrdinalIgnoreCase) >= 0) + else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A4", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("landscape", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A4H.pc5", null); } - else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A3及landscape", StringComparison.OrdinalIgnoreCase) >= 0) + else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A3", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("landscape", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A3H.pc5", null); } - else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A2及landscape", StringComparison.OrdinalIgnoreCase) >= 0) + else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A2", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("landscape", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A2H.pc5", null); } - else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A1及landscape", StringComparison.OrdinalIgnoreCase) >= 0) + else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A1", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("landscape", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A1H.pc5", null); } @@ -152,7 +152,6 @@ namespace connor_zwcadm.commands psv.SetPlotConfigurationName(ps, "DWG to PDF.pc5", null); } - flag = false; break; } @@ -161,23 +160,23 @@ namespace connor_zwcadm.commands if (flag) { - if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A4及portrait", StringComparison.OrdinalIgnoreCase) >= 0) + if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A4", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("portrait", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A4S.pc5", null); } - else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A4及landscape", StringComparison.OrdinalIgnoreCase) >= 0) + else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A4", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("landscape", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A4H.pc5", null); } - else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A3及landscape", StringComparison.OrdinalIgnoreCase) >= 0) + else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A3", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("landscape", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A3H.pc5", null); } - else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A2及landscape", StringComparison.OrdinalIgnoreCase) >= 0) + else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A2", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("landscape", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A2H.pc5", null); } - else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A1及landscape", StringComparison.OrdinalIgnoreCase) >= 0) + else if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("A1", StringComparison.OrdinalIgnoreCase) >= 0 && plotData.TitleBlockName.IndexOf("landscape", StringComparison.OrdinalIgnoreCase) >= 0) { psv.SetPlotConfigurationName(ps, "A1H.pc5", null); } diff --git a/connor_zwcadm/commands/SyncTitleCommand.cs b/connor_zwcadm/commands/SyncTitleCommand.cs index 515bb82..d23cae9 100644 --- a/connor_zwcadm/commands/SyncTitleCommand.cs +++ b/connor_zwcadm/commands/SyncTitleCommand.cs @@ -172,92 +172,95 @@ namespace connor_zwcadm.commands { Dictionary cadProps = tcUtil.ReadTCData2(titleData,th,bb); - KUtil.Log("--------------1" + KUtil.PrintDictionary(cadProps)); + if (cadProps != null) { + KUtil.Log("--------------1" + KUtil.PrintDictionary(cadProps)); - using (Transaction tran = db.TransactionManager.StartTransaction()) - { - KUtil.Log("3. 开始事务"); - BlockTable blt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; - - KUtil.Log("4. 获取块表"); - // 注意:这里只遍历了当前空间 (CurrentSpaceId)。 - // 如果标题栏在布局(Layout)中而当前在模型空间,可能遍历不到。 - // 如果需要遍历所有布局,逻辑需要扩展(见下方提示)。 - BlockTableRecord bltr = tran.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord; - KUtil.Log("5. 开始遍历实体"); - foreach (ObjectId item in bltr) + using (Transaction tran = db.TransactionManager.StartTransaction()) { - // 先以只读方式打开,提高性能 - Entity ent = tran.GetObject(item, OpenMode.ForRead) as Entity; + KUtil.Log("3. 开始事务"); + BlockTable blt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; - // 优化判断:直接使用 is 关键字,比 GetType().Name 更安全高效 - if (ent is BlockReference bref) + KUtil.Log("4. 获取块表"); + // 注意:这里只遍历了当前空间 (CurrentSpaceId)。 + // 如果标题栏在布局(Layout)中而当前在模型空间,可能遍历不到。 + // 如果需要遍历所有布局,逻辑需要扩展(见下方提示)。 + BlockTableRecord bltr = tran.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord; + + KUtil.Log("5. 开始遍历实体"); + foreach (ObjectId item in bltr) { + // 先以只读方式打开,提高性能 + Entity ent = tran.GetObject(item, OpenMode.ForRead) as Entity; - if (bref.Name.Contains("更改栏")) + // 优化判断:直接使用 is 关键字,比 GetType().Name 更安全高效 + if (ent is BlockReference bref) { - - KUtil.Log("发现块引用:" + bref.Name); - - if (bref.AttributeCollection.Count > 0) + if (bref.Name.Contains("更改栏")) { - // 遍历属性集合 - foreach (ObjectId attId in bref.AttributeCollection) - { - // 先以只读方式打开属性 - AttributeReference aRef = tran.GetObject(attId, OpenMode.ForRead) as AttributeReference; - if (aRef != null) - { + KUtil.Log("发现块引用:" + bref.Name); + if (bref.AttributeCollection.Count > 0) + { + // 遍历属性集合 + foreach (ObjectId attId in bref.AttributeCollection) + { + // 先以只读方式打开属性 + AttributeReference aRef = tran.GetObject(attId, OpenMode.ForRead) as AttributeReference; - foreach (KeyValuePair item2 in cadProps) + if (aRef != null) { - string key = item2.Key; // 属性标签 (Tag),例如 "PROJECT_NAME" - string value = item2.Value; // 属性值 (TextString),例如 "某某大厦项目" - // 【核心逻辑】检查 Tag 是否为 "编制" - // 注意:Tag 区分大小写,请确认 CAD 中该属性的 Tag 确实是 "编制" - // 有时可能是 "DESIGNER", "DRAWN_BY" 等,如果不确定可以先打印 aRef.Tag 看看 - if (aRef.Tag.Equals(key)) - { - // 【关键步骤】升级为写模式,否则无法赋值 - aRef.UpgradeOpen(); - // 设置新值 - aRef.TextString = value; - KUtil.Log($"已修改块 '{bref.Name}' ======: {value}"); - // 如果只需要修改第一个找到的,可以在这里 break; - // 如果要修改所有符合条件的块,则继续循环 + foreach (KeyValuePair item2 in cadProps) + { + string key = item2.Key; // 属性标签 (Tag),例如 "PROJECT_NAME" + string value = item2.Value; // 属性值 (TextString),例如 "某某大厦项目" + // 【核心逻辑】检查 Tag 是否为 "编制" + // 注意:Tag 区分大小写,请确认 CAD 中该属性的 Tag 确实是 "编制" + // 有时可能是 "DESIGNER", "DRAWN_BY" 等,如果不确定可以先打印 aRef.Tag 看看 + if (aRef.Tag.Equals(key)) + { + // 【关键步骤】升级为写模式,否则无法赋值 + aRef.UpgradeOpen(); + + // 设置新值 + aRef.TextString = value; + + KUtil.Log($"已修改块 '{bref.Name}' ======: {value}"); + + // 如果只需要修改第一个找到的,可以在这里 break; + // 如果要修改所有符合条件的块,则继续循环 + } } - } - - + + + } } } } } } + + // 提交事务,保存修改 + tran.Commit(); + KUtil.Log("事务已提交,属性修改生效"); } - // 提交事务,保存修改 - tran.Commit(); - KUtil.Log("事务已提交,属性修改生效"); } - KUtil.Log("转换PDF完成(属性已预设置)"); return path; } diff --git a/connor_zwcadm/util/TCUtil.cs b/connor_zwcadm/util/TCUtil.cs index 8500a03..f99190b 100644 --- a/connor_zwcadm/util/TCUtil.cs +++ b/connor_zwcadm/util/TCUtil.cs @@ -479,6 +479,7 @@ namespace connor_zwcadm.util { KUtil.Log(string.Format("查询TC数据:类型[{0}] ID[{1}] 版本[{2}] 名称[{3}]", itemTypeName, itemId, revId, "")); ItemRevision rev = null; + ItemRevision rev2 = null; if (string.IsNullOrWhiteSpace(itemId)) { throw new Exception("参数异常:对象ID不可为空"); @@ -519,17 +520,17 @@ namespace connor_zwcadm.util { KUtil.Log("referencedObject.Object_type:" + referencedObject.Object_type); if (referencedObject.Object_type.Equals("Lb5_ChangeNoticeRevision")) { - rev = (ItemRevision)referencedObject; + rev2 = (ItemRevision)referencedObject; } } } + if (rev2 != null) { + data.Rev = rev2; + } - GetProperties(rev, "object_string"); - KUtil.Log("找到已存在的版本:" + rev.Object_string); - //更新属性 - data.Rev = rev; + return ReadProperties(data); }