基于文件内容,能否帮我优化下pictureBox17_Click
时间: 2024-12-17 19:53:35 浏览: 6
当然可以!以下是 `pictureBox17_Click` 方法的优化版本。主要优化点包括代码结构简化、注释增加以提高可读性,并减少不必要的重复代码:
```csharp
private void pictureBox17_Click(object sender, EventArgs e)
{
try
{
// 获取输入参数
double sideDistance = double.Parse(textBox12.Text);
double depth1 = double.Parse(textBox5.Text.Trim());
double depth2 = double.Parse(textBox2.Text.Trim());
// 获取当前选择的图形
DrawingSelector drawingSelector = dh.GetDrawingSelector();
DrawingEnumerator drawEnum = drawingSelector.GetSelected();
while (drawEnum.MoveNext())
{
Drawing drawing = drawEnum.Current as Drawing;
dh.SetActiveDrawing(drawing);
// 获取装配件和主梁部分
AssemblyDrawing partDraw = drawing as AssemblyDrawing;
Identifier assemblyId = partDraw.AssemblyIdentifier;
Assembly assembly = model.SelectModelObject(assemblyId) as Assembly;
if (assembly == null) return;
TSM.Part mainPart = assembly.GetMainPart() as TSM.Part;
double sheetWidth = partDraw.Layout.SheetSize.Width;
double sheetHeight = partDraw.Layout.SheetSize.Height;
// 获取所有视图
DrawingObjectEnumerator viewEnum = partDraw.GetSheet().GetAllViews();
if (viewEnum.GetSize() <= 0) return;
while (viewEnum.MoveNext())
{
TSD.View currentView = viewEnum.Current as TSD.View;
if (currentView == null) continue;
// 获取边界点
TSG.Point leftMaxPoint, rightMaxPoint, leftMinPoint, rightMinPoint;
if (Weldh_checkBox1.Checked)
{
var selectedParts = Get_Selector_TSMparts(out _, out _, out _);
if (selectedParts.Count == 0)
{
MessageBox.Show("请在图纸中选择三块板", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
AllParts_AABB(currentView, selectedParts, out leftMinPoint, out leftMaxPoint, out rightMinPoint, out rightMaxPoint);
}
else
{
GetAABBpoint(currentView, mainPart, out leftMinPoint, out leftMaxPoint, out rightMinPoint, out rightMaxPoint);
}
// 创建剖面视图
if (checkBox23.Checked)
{
TSD.View sectionView = null;
CreateView.CreatePouMianView(currentView, out sectionView, "shang");
Delta_View_Make(sectionView);
DingShiTuBiaoZhu(sectionView);
}
// 删除现有尺寸和标记
if (checkBox22.Checked)
{
DeleteExistingMarksAndDimensions(currentView);
}
// 处理尺寸标注
HandleDimensions(currentView, mainPart, leftMaxPoint, rightMaxPoint, leftMinPoint, rightMinPoint);
// 更新视图位置
UpdateViewPosition(currentView, sheetWidth, sheetHeight);
// 提交更改
drawing.CommitChanges();
}
}
}
catch (Exception ex)
{
MessageBox.Show($"发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void DeleteExistingMarksAndDimensions(TSD.View view)
{
ArrayList objectsToDelete = new ArrayList();
DrawingObjectEnumerator viewObjsEnum = view.GetAllObjects();
while (viewObjsEnum.MoveNext())
{
if (checkBox15.Checked && viewObjsEnum.Current is TSD.StraightDimension ||
checkBox16.Checked && (viewObjsEnum.Current is Mark || viewObjsEnum.Current is MarkSet || viewObjsEnum.Current is TSD.Line))
{
objectsToDelete.Add(viewObjsEnum.Current);
}
}
dh.GetDrawingObjectSelector().SelectObjects(objectsToDelete, true);
string dllPath = "";
TeklaStructuresSettings.GetAdvancedOption("XS_MACRO_DIRECTORY", ref dllPath);
string[] macroPaths = Regex.Split(dllPath, ";", RegexOptions.IgnoreCase);
Functions.WriteBoxMacro(macroPaths[1], "Delta_make.cs", Functions.Delta_make());
Operation.RunMacro("Delta_make.cs");
}
private void HandleDimensions(TSD.View view, TSM.Part mainPart, TSG.Point leftMaxPoint, TSG.Point rightMaxPoint, TSG.Point leftMinPoint, TSG.Point rightMinPoint)
{
double vector2 = radioButton14.Checked || radioButton12.Checked ? 1 : radioButton13.Checked || radioButton15.Checked ? -1 : 0;
if (vector2 == 0) return;
// 获取需要标注的零件和螺栓
ArrayList onlySelectorParts = GetFilteredParts(view);
if (onlySelectorParts == null || onlySelectorParts.Count == 0) return;
// 标注尺寸
StraightDimensionSet.StraightDimensionSetAttributes sdSetAttr = new StraightDimensionSet.StraightDimensionSetAttributes(Public_Size_Att.Text);
sdSetAttr.Placing.Placing = DimensionSetBaseAttributes.Placings.Fixed;
StraightDimensionSetHandler sdSet = new StraightDimensionSetHandler();
if (checkBox13.Checked)
{
CreatePartAnnotations(view, onlySelectorParts, sdSetAttr);
}
if (checkBox14.Checked)
{
CreatePartDimensions(view, onlySelectorParts, leftMaxPoint, rightMaxPoint, sdSetAttr);
}
if (checkBox1.Checked)
{
CreateBoltDimensions(view, mainPart, leftMaxPoint, rightMaxPoint, leftMinPoint, rightMinPoint, sdSetAttr);
}
// 总尺寸
PointList totalPoints = new PointList { leftMaxPoint, rightMaxPoint };
CreateDimensionSet(view, totalPoints, CalculateDistance(sdSetAttr, view), sdSetAttr, "SHANG");
// 其他尺寸
PointList otherPoints = new PointList { leftMinPoint, rightMinPoint };
CreateDimensionSet(view, otherPoints, CalculateDistance(sdSetAttr, view), sdSetAttr, "XIA");
}
private ArrayList GetFilteredParts(TSD.View view)
{
string[] excludeNames = ChaZhao_textBox3.Text.Split(' ');
string[] excludeSections = ChaZhao_textBox2.Text.Split(' ');
string[] includeNames = ShanNeng_XuanZe1.Text.Split(' ');
string[] includeSections = ChaZhao_textBox9.Text.Split(' ');
if (includeNames.Length > 0 || includeSections.Length > 0)
{
return Get_OnlySelector_TSMparts(view, includeNames, includeSections, out _);
}
else
{
return Get_Selector_TSMparts(view, excludeNames, excludeSections);
}
}
private void CreatePartAnnotations(TSD.View view, ArrayList parts, StraightDimensionSet.StraightDimensionSetAttributes sdSetAttr)
{
dh.GetDrawingObjectSelector().SelectObjects(parts, true);
string dllPath = "";
TeklaStructuresSettings.GetAdvancedOption("XS_MACRO_DIRECTORY", ref dllPath);
string[] macroPaths = Regex.Split(dllPath, ";", RegexOptions.IgnoreCase);
Functions.WriteBoxMacro(macroPaths[0], "Create_make.cs", Functions.Create_Make());
Operation.RunMacro("Create_make.cs");
dh.GetDrawingObjectSelector().UnselectObjects(parts);
}
private void CreatePartDimensions(TSD.View view, ArrayList parts, TSG.Point leftMaxPoint, TSG.Point rightMaxPoint, StraightDimensionSet.StraightDimensionSetAttributes sdSetAttr)
{
PointList points = new PointList();
foreach (TSM.Part part in parts)
{
TSG.Point partLeftMax, partRightMax;
GetAABBpoint(view, part, out _, out partLeftMax, out _, out partRightMax);
points.Add(radioButton19.Checked ? partRightMax : partLeftMax);
}
PointList totalPoints = new PointList { leftMaxPoint, rightMaxPoint };
CreateDimensionSet(view, points, CalculateDistance(sdSetAttr, view), sdSetAttr, "SHANG");
}
private void CreateBoltDimensions(TSD.View view, TSM.Part mainPart, TSG.Point leftMaxPoint, TSG.Point rightMaxPoint, TSG.Point leftMinPoint, TSG.Point rightMinPoint, StraightDimensionSet.StraightDimensionSetAttributes sdSetAttr)
{
List<PointList> boltPoints = GetBoltsPointList2(view, mainPart, out _, out _);
PointList points = new PointList();
foreach (PointList group in boltPoints)
{
points.AddRange(group);
}
if (YDim1_checkBox1.Checked)
{
CreateVerticalBoltDimensions(view, leftMaxPoint, rightMaxPoint, leftMinPoint, rightMinPoint, points, sdSetAttr);
}
// 总尺寸
PointList totalPoints = new PointList { leftMaxPoint, rightMaxPoint };
CreateDimensionSet(view, totalPoints, CalculateDistance(sdSetAttr, view), sdSetAttr, "SHANG");
}
private void CreateVerticalBoltDimensions(TSD.View view, TSG.Point leftMaxPoint, TSG.Point rightMaxPoint, TSG.Point leftMinPoint, TSG.Point rightMinPoint, PointList points, StraightDimensionSet.StraightDimensionSetAttributes sdSetAttr)
{
double distance1 = CalculateDistance(sdSetAttr, view);
double distance2 = 2 * double.Parse(DIM_textH2.Text) * view.Attributes.Scale;
foreach (TSG.Point boltPoint in points)
{
PointList verticalPoints = new PointList();
verticalPoints.Add(new TSG.Point(boltPoint.X, leftMaxPoint.Y, 0));
verticalPoints.Add(new TSG.Point(boltPoint.X, rightMinPoint.Y, 0));
verticalPoints.Add(boltPoint);
CreateDimensionSet(view, verticalPoints, distance1 + distance2, sdSetAttr, "SHANG");
}
}
private double CalculateDistance(StraightDimensionSet.StraightDimensionSetAttributes sdSetAttr, TSD.View view)
{
double textHeight = double.Parse(DIM_textH2.Text);
double distance = DIM_textBox1.Text != "尺寸距离" && Regex.IsMatch(DIM_textBox1.Text, @"^\d+$")
? double.Parse(DIM_textBox1.Text)
: 10;
return (textHeight + distance) * view.Attributes.Scale;
}
private void UpdateViewPosition(TSD.View view, double sheetWidth, double sheetHeight)
{
TSG.Point leftLowerPt = new TSG.Point(0.5 * (sheetWidth - 75), 120, 0);
TSG.Point leftLowerPt2 = Geometry.VpointL(new Vector(1, 0, 0), leftLowerPt, -0.5 * view.Width);
view.Origin = new TSG.Point(leftLowerPt2.X - view.FrameOrigin.X, leftLowerPt2.Y - view.FrameOrigin.Y, 0);
view.Modify();
double viewHeight = view.Height;
leftLowerPt = Geometry.VpointL(new Vector(0, 1, 0), leftLowerPt, viewHeight);
TSG.Point leftLowerPt3 = Geometry.VpointL(new Vector(1, 0, 0), leftLowerPt, -0.5 * view.Width);
view.Origin = new TSG.Point(leftLowerPt3.X - view.FrameOrigin.X, leftLowerPt3.Y - view.FrameOrigin.Y, 0);
view.Modify();
}
```
### 主要改进点:
1. **代码结构简化**:将复杂的逻辑拆分为多个小函数,每个函数负责一个特定的任务。
2. **注释增加**:增加了必要的注释,以便更好地理解每一部分的功能。
3. **异常处理**:添加了异常处理机制,捕获并显示可能发生的错误。
4. **减少重复代码**:避免了多次出现的相同代码段,提高了代码的复用性和可维护性。
希望这些改进能帮助你更好地管理和维护代码。如果有任何进一步的问题或需求,请随时告知!
阅读全文