C#报错 功能“结构字段初始化表达式“在C#7.3中不可用。请使用10.0或更高的语言版本。

本文介绍了如何在XML文件中增加<LangVersion>10.0</LangVersion>标签来升级代码语言版本,以解决技术问题。详细步骤指导了相关调整过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决方式:

打开以下文件

 增加  <LangVersion>10.0</LangVersion>

using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Windows.Forms; namespace BeamAnnotationPlugin { public class BeamProcessor { // 图层配置 private static string _beamLayer = ""; private static string _dimLayer = ""; // 标注设置 private static double _textHeight = 2.5; private static double _elevationOffset = 0.05; } } public class BeamMark { public string Name { get; set; } = ""; public string Section { get; set; } = ""; public double Elevation { get; set; } = 0; public Point3d Position { get; set; } public bool IsCentralMark { get; set; } = true; } [CommandMethod("BR", CommandFlags.Modal)] public void BeamRecognition() { // 识别集中标注和原位标注 PromptSelectionResult selRes = _ed.GetSelection(); if (selRes.Status != PromptStatus.OK) return; using (Transaction tr = _db.TransactionManager.StartTransaction()) { List<BeamMark> beamMarks = new List<BeamMark>(); foreach (ObjectId id in selRes.Value.GetObjectIds()) { Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity; if (ent is DBText text && ent.Layer == _beamLayer) { BeamMark mark = ParseBeamText(text.TextString); mark.Position = text.Position; beamMarks.Add(mark); } } // 存储识别结果 StoreBeamData(beamMarks); tr.Commit(); } } private BeamMark ParseBeamText(string text) { // 使用正则表达式解析梁标注 BeamMark mark = new BeamMark(); // 匹配格式: KL1(2) 300x700 (-0.050) Regex regex = new Regex(@"(\w+)\s*$(\d+)$\s*(\d+)x(\d+)\s*(?:$([-]?\d+\.\d+)$)?"); Match match = regex.Match(text); if (match.Success) { mark.Name = match.Groups[1].Value + "(" + match.Groups[2].Value + ")"; mark.Section = $"{match.Groups[3].Value}×{match.Groups[4].Value}"; if (match.Groups.Count > 5 && !string.IsNullOrEmpty(match.Groups[5].Value)) { mark.Elevation = double.Parse(match.Groups[5].Value); } } return mark; } [CommandMethod("AG", CommandFlags.Modal)] public void AutoGenerateMarks() { PromptPointResult ptRes = _ed.GetPoint("\n选择标注插入点: "); if (ptRes.Status != PromptStatus.OK) return; using (Transaction tr = _db.TransactionManager.StartTransaction()) { BlockTableRecord btr = (BlockTableRecord)tr.GetObject( _db.CurrentSpaceId, OpenMode.ForWrite); // 生成集中标注 DBText centralMark = new DBText { Position = ptRes.Value, TextString = FormatBeamText(_currentBeam, true), Height = _textHeight, Layer = _dimLayer }; btr.AppendEntity(centralMark); tr.AddNewlyCreatedDBObject(centralMark, true); // 生成原位标注 Point3d locPos = new Point3d( ptRes.Value.X + 20, ptRes.Value.Y, ptRes.Value.Z); DBText localMark = new DBText { Position = locPos, TextString = FormatBeamText(_currentBeam, false), Height = _textHeight * 0.8, Layer = _dimLayer }; btr.AppendEntity(localMark); tr.AddNewlyCreatedDBObject(localMark, true); tr.Commit(); } } private string FormatBeamText(BeamMark beam, bool isCentral) { // 集中标注格式: KL1(2) 300x700 // 原位标注格式: 300x700 (-0.050) if (isCentral) { return $"{beam.Name} {beam.Section}"; } else { return beam.Elevation != 0 ? $"{beam.Section} ({beam.Elevation:F3})" : beam.Section; } } [CommandMethod("BST", CommandFlags.Modal)] public void BeamStatistics() { List<BeamStatItem> stats = CalculateBeamStatistics(); // 按荷载排序: $ q = \frac{w \times h}{10^6} \times 25 $ stats.Sort((a, b) => b.Load.CompareTo(a.Load)); CreateStatisticsTable(stats); } private List<BeamStatItem> CalculateBeamStatistics() { List<BeamStatItem> stats = new List<BeamStatItem>(); foreach (BeamMark beam in _recognizedBeams) { // 解析截面尺寸 string[] dims = beam.Section.Split(&#39;×&#39;); if (dims.Length != 2) continue; if (double.TryParse(dims[0], out double width) && double.TryParse(dims[1], out double height)) { // 计算荷载: $ q = A \times \gamma $ double area = (width * height) / 1_000_000.0; double load = area * 25; // γ=25kN/m&sup3; stats.Add(new BeamStatItem { Name = beam.Name, Section = beam.Section, Elevation = beam.Elevation, Load = load }); } } return stats; } [CommandMethod("DR", CommandFlags.Modal)] public void DeleteReinforcement() { using (Transaction tr = _db.TransactionManager.StartTransaction()) { // 创建钢筋标注特征过滤器 TypedValue[] filterList = { new TypedValue((int)DxfCode.Start, "TEXT"), new TypedValue((int)DxfCode.LayerName, _dimLayer), new TypedValue((int)DxfCode.Text, "*Φ*") // 匹配钢筋符号 }; SelectionFilter filter = new SelectionFilter(filterList); PromptSelectionResult selRes = _ed.SelectAll(filter); if (selRes.Status == PromptStatus.OK) { foreach (ObjectId id in selRes.Value.GetObjectIds()) { Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity; ent.Erase(); } } tr.Commit(); } } [CommandMethod("SE", CommandFlags.Modal)] public void SetElevationOffset() { PromptDoubleOptions opts = new PromptDoubleOptions("\n输入标高偏移值(m): "); opts.DefaultValue = _elevationOffset; opts.AllowNegative = true; PromptDoubleResult res = _ed.GetDouble(opts); if (res.Status == PromptStatus.OK) { _elevationOffset = res.Value; _ed.WriteMessage($"\n标高偏移已设置为: {_elevationOffset:F3}m"); } } private double CalculateAdjustedElevation(double original) { // 应用标高偏移: $ E_{adj} = E_{orig} + \Delta $ return original + _elevationOffset; } [CommandMethod("FS", CommandFlags.Modal)] public void AdjustTextSize() { PromptDoubleOptions opts = new PromptDoubleOptions("\n输入标注文字高度: "); opts.DefaultValue = _textHeight; opts.AllowNegative = false; PromptDoubleResult res = _ed.GetDouble(opts); if (res.Status == PromptStatus.OK) { _textHeight = res.Value; _ed.WriteMessage($"\n文字高度已设置为: {_textHeight}"); } } 以上代码出现的问题 CS0234命名空间“System.Windows”中存在类型命名空间名“Forms”(是否缺少程序集引用?) CS8803顶级语句必须位于命名空间和类型声明之前。 CS8370功能”顶级语句”在C#7.3可用使用9.0更高语言版本。 CS8370功能”本地函数特性”在C#7.3可用使用9.0更高语言版本。 CS0106修饰符public”对该项无效 CS0103当前上下文中存在名称_ed” CS0103当前上下文中存在名称_db" CS0103当前上下文中存在名称_beamLayer CS0103当前上下文中存在名称StoreBeamData” CS0106修饰符private”对该项无效 CS8370功能”本地函数特性”在C#7.3可用使用9.0更高语言版本。 CS0106修饰符“public"对该项无效 CS0103当前上下文中存在名称ed CS0103当前上下文中存在名称_db” CS0103当前上下文中存在名称_db” CS0103当前上下文中存在名称_currentBeam” CS0103当前上下文中存在名称textHeight CS0103当前上下文中存在名称_dimLayer” CS0103当前上下文中存在名称_currentBeam” CS0103当前上下文中存在名称textHeight CS0103当前上下文中存在名称_dimLayer CS0106修饰符private"对该项无效 CS8370功能”本地函数特性”在C#7.3可用使用9.0更高语言版本。 CS0106修饰符public对该项无效 CS0246未能找到类型命名空间名“BeamStatltem”(是否缺少using指令程序集引用?) CS0103当前上下文中存在名称“CreateStatisticsTable” CS0106修饰符private"对该项无效 CS0246未能找到类型命名空间名“BeamStatltem”(是否缺少using 指令程序集引用?) CS0246未能找到类型命名空间名“BeamStatltem”(是否缺少using 指令程序集引用?) CS0246未能找到类型命名空间名“BeamStatltem”(是否缺少using 指令程序集引用?) CS0103当前上下文中存在名称_recognizedBeams CS0103当前上下文中存在名称“area” CS0246未能找到类型命名空间名“BeamStatltem”(是否缺少using 指令程序集引用?) CS0246未能找到类型命名空间名“BeamStatltem”(是否缺少using指令程序集引用?) CS8370功能”本地函数特性”在C#7.3可用使用9.0更高语言版本。 CS0106修饰符public对该项无效 CS0103当前上下文中存在名称_db CS0103当前上下文中存在名称dimLayer" CS0103当前上下文中存在名称_ed” CS8370功能”本地函数特性”在C#7.3可用使用9.0更高语言版本。 CS0106修饰符public"对该项无效 CS0103当前上下文中存在名称_elevationOffset CS0103当前上下文中存在名称_ed” CS0103当前上下文中存在名称_elevationOffset CS0103当前上下文中存在名称_ed” CS0103当前上下文中存在名称elevationOffset CS0106修饰符“private”对该项无效 CS0161“CalculateAdjustedElevation(double)”:并非所有的代码路径都返回值 CS8370功能”本地函数特性”在C#7.3可用使用9.0更高语言版本。 CS0106修饰符“public对该项无效 CS0103当前上下文中存在名称_textHeight CS0103当前上下文中存在名称_ed” CS0103当前上下文中存在名称_textHeight CS0103当前上下文中存在名称ed CS0103当前上下文中存在名称textHeight CS8321声明了本地函数BeamRecognition”,但从未使用过 CS8321声明了本地函数AutoGenerateMarks”,但从未使用过 CS8321声明了本地函数BeamStatistics”,但从未使用过 CS8321声明了本地函数DeleteReinforcement”,但从未使用过 CS8321声明了本地函数SetElevationOffset”,但从未使用过 CS8321声明了本地函数“CalculateAdjustedElevation”,但从未使用过 CS8321声明了本地函数AdjustTextSize”,但从未使用
最新发布
07-04
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using System; using System.Collections.Generic; using System.Linq; namespace BeamPlugin { public class BeamCommands { // 梁信息存储类 private class BeamInfo { public string Name { get; set; } public string Size { get; set; } public double? Elevation { get; set; } // 标高值(单位:米) } // 主命令:识别并标注梁 (缩写为 BD) [CommandMethod("BD")] public void BeamDimensioning() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; using (Transaction tr = db.TransactionManager.StartTransaction()) { try { // 1. 点选识别集中标注和原位标注 var beamDict = new Dictionary<ObjectId, BeamInfo>(); IdentifyBeams(ed, tr, beamDict); // 2. 框选识别所有梁并标注 PromptSelectionResult selRes = ed.GetSelection(); if (selRes.Status == PromptStatus.OK) { SelectionSet selSet = selRes.Value; ProcessBeams(tr, selSet, beamDict); } // 3. 清理需要的图层 CleanupLayers(db); // 传递Database对象 tr.Commit(); } catch (System.Exception ex) // 明确指定System.Exception { ed.WriteMessage($"\n错误: {ex.Message}"); } } } // 识别梁信息 private void IdentifyBeams(Editor ed, Transaction tr, Dictionary<ObjectId, BeamInfo> beamDict) { // 点选集中标注(假设为文字实体) PromptEntityOptions opt = new PromptEntityOptions("\n选择梁集中标注: "); opt.SetRejectMessage("\n选择文字对象"); opt.AddAllowedClass(typeof(DBText), false); PromptEntityResult res = ed.GetEntity(opt); if (res.Status == PromptStatus.OK) { DBText dimText = tr.GetObject(res.ObjectId, OpenMode.ForRead) as DBText; BeamInfo info = ParseBeamText(dimText.TextString); beamDict.Add(dimText.OwnerId, info); // 关联到梁实体 } // 点选原位标注(类似逻辑) opt.Message = "\n选择梁原位标注: "; res = ed.GetEntity(opt); if (res.Status == PromptStatus.OK) { DBText dimText = tr.GetObject(res.ObjectId, OpenMode.ForRead) as DBText; BeamInfo info = ParseBeamText(dimText.TextString); beamDict.Add(dimText.OwnerId, info); } } // 解析梁文本信息 private BeamInfo ParseBeamText(string text) { var info = new BeamInfo(); // 示例解析逻辑 if (text.Contains(";")) { string[] parts = text.Split(&#39;;&#39;); info.Name = parts[0].Trim(); if (parts.Length > 1 && double.TryParse(parts[1], out double elev)) info.Elevation = elev; } else { info.Name = text; } // 从名称中提取尺寸(示例:2-Lg22(6) 600x800) if (info.Name.Contains(" ")) { string[] nameParts = info.Name.Split(&#39; &#39;); info.Name = nameParts[0]; if (nameParts.Length > 1 && nameParts[1].Contains(&#39;x&#39;)) { info.Size = nameParts[1]; } } return info; } // 处理梁并标注 private void ProcessBeams(Transaction tr, SelectionSet selSet, Dictionary<ObjectId, BeamInfo> beamDict) { BlockTable bt = (BlockTable)tr.GetObject(tr.Database.BlockTableId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); foreach (SelectedObject selObj in selSet) { if (selObj != null) { Entity ent = tr.GetObject(selObj.ObjectId, OpenMode.ForRead) as Entity; if (ent != null && ent is Polyline && ent.Layer == "梁") // 假设梁在"梁"图层 { Polyline beam = ent as Polyline; BeamInfo info = FindBeamInfo(beam, beamDict); if (info != null) { // 创建梁标注 DBText label = CreateBeamLabel(beam, info); btr.AppendEntity(label); tr.AddNewlyCreatedDBObject(label, true); } } } } } // 查找梁信息(支持同名匹配) private BeamInfo FindBeamInfo(Polyline beam, Dictionary<ObjectId, BeamInfo> beamDict) { // 1. 尝试直接获取 if (beamDict.TryGetValue(beam.ObjectId, out BeamInfo directInfo)) return directInfo; // 2. 通过同名匹配 string beamName = GetBeamName(beam); if (!string.IsNullOrEmpty(beamName)) { return beamDict.Values.FirstOrDefault(v => v.Name.StartsWith(beamName)); } return null; } // 获取梁名称(从未标注梁中提取) private string GetBeamName(Polyline beam) { // 实际项目中应根据图层/扩展数据等获取 // 示例:从图层名提取(图层格式为"梁-名称") return beam.Layer.Contains(&#39;-&#39;) ? beam.Layer.Split(&#39;-&#39;).Last() : beam.Layer; } // 获取梁中点坐标 (修复GetPointAtDist参数问题) private Point3d GetBeamMidPoint(Polyline beam) { double halfLength = beam.Length / 2; return beam.GetPointAtDist(halfLength); } // 估算文字宽度 private double EstimateTextWidth(string text, double height) { // 近似计算:每个字符宽度=高度的0.7倍 return text.Length * height * 0.7; } // 创建梁标注文字 private DBText CreateBeamLabel(Polyline beam, BeamInfo info) { Point3d midPoint = GetBeamMidPoint(beam); double textHeight = 150; // 标注高度 DBText label = new DBText { Position = midPoint, TextString = FormatLabelText(info, beam.Length), Height = textHeight, Layer = "梁标注", AlignmentPoint = midPoint, Justify = AttachmentPoint.MiddleCenter }; // 自动调整文字大小 double textWidth = EstimateTextWidth(label.TextString, textHeight); if (textWidth > beam.Length * 0.8) // 超过梁长的80% { label.Height = textHeight * (beam.Length * 0.8 / textWidth); } return label; } // 格式化标注文本 private string FormatLabelText(BeamInfo info, double beamLength) { string label = info.Name; if (!string.IsNullOrEmpty(info.Size)) label += " " + info.Size; if (info.Elevation.HasValue) { label += $";{info.Elevation.Value.ToString(info.Elevation.Value < 0 ? "0.00" : "0")}"; } return label; } // 清理图层 private void CleanupLayers(Database db) { using (Transaction tr = db.TransactionManager.StartTransaction()) { LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead); // 要保留的图层 string[] keepLayers = { "轴网", "轴号", "梁", "墙", "柱", "尺寸标注", "填充" }; foreach (ObjectId layerId in lt) { LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(layerId, OpenMode.ForRead); if (!keepLayers.Contains(ltr.Name)) { // 删除非保留图层上的所有实体 DeleteEntitiesOnLayer(tr, db, ltr.Name); } } tr.Commit(); } } // 删除指定图层上的所有实体 (修复遍历问题) private void DeleteEntitiesOnLayer(Transaction tr, Database db, string layerName) { BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); // 使用安全方式遍历 ObjectIdCollection idsToErase = new ObjectIdCollection(); foreach (ObjectId id in btr) { Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity; if (ent != null && ent.Layer == layerName) { idsToErase.Add(id); } } // 单独执行删除 foreach (ObjectId id in idsToErase) { Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity; if (ent != null && !ent.IsErased) { ent.Erase(); } } } // 统计梁截面命令 (缩写为 BS) [CommandMethod("BS")] public void BeamStatistics() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptSelectionResult selRes = ed.GetSelection(); if (selRes.Status != PromptStatus.OK) return; using (Transaction tr = db.TransactionManager.StartTransaction()) { try { // 统计梁截面尺寸 var sizeStats = new Dictionary<string, int>(); foreach (SelectedObject selObj in selRes.Value) { Entity ent = tr.GetObject(selObj.ObjectId, OpenMode.ForRead) as Entity; if (ent is Polyline && ent.Layer == "梁") { // 从梁标注中提取尺寸 string beamSize = ExtractBeamSize(ent); if (!string.IsNullOrEmpty(beamSize)) { if (sizeStats.ContainsKey(beamSize)) { sizeStats[beamSize]++; } else { sizeStats[beamSize] = 1; } } } } // 生成统计表格 GenerateStatisticsTable(ed, tr, sizeStats); tr.Commit(); } catch (System.Exception ex) { ed.WriteMessage($"\n错误: {ex.Message}"); } } } private string ExtractBeamSize(Entity ent) { // 实际实现从梁实体标注中提取尺寸 // 这里返回模拟数据 return "600x800"; } private void GenerateStatisticsTable(Editor ed, Transaction tr, Dictionary<string, int> sizeStats) { // 创建统计表格 ed.WriteMessage("\n梁截面尺寸统计:"); foreach (var kvp in sizeStats.OrderBy(x => x.Key)) { ed.WriteMessage($"\n尺寸 {kvp.Key}: {kvp.Value} 根"); } } } } S1061“Transaction”未包含”Database”的定义,并且找到可接受第一个“Transaction”类型参数的可访问扩展方法”Database”(是否缺少using 指令程序集引用?
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值