Место общения программистов C#
Anonymous
Как исправить тип исключения в C#.net ObjectArx
Сообщение
Anonymous » 13 янв 2025, 12:34
когда я запускаю свой код, у меня всегда есть эта строка. Было выдано исключение типа Autodesk.AutoCAD.BoundaryRepresentation.Exception.
я использую .netFramework 4.8 в Visual Studio 2022, есть ли у кого-нибудь такая же ошибка или кто-нибудь знает, как ее исправить? это
вот код
Код: Выделить всё
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.BoundaryRepresentation;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
namespace _3Dinterface.ViewModels
{
public class MainWindowViewModel : INotifyPropertyChanged
{
private string _selectedColor1;
private string _selectedColor2;
private Autodesk.AutoCAD.Colors.Color _color1;
private Autodesk.AutoCAD.Colors.Color _color2;
public string SelectedColor1
{
get => _selectedColor1;
set { _selectedColor1 = value; OnPropertyChanged(nameof(SelectedColor1)); }
}
public string SelectedColor2
{
get => _selectedColor2;
set { _selectedColor2 = value; OnPropertyChanged(nameof(SelectedColor2)); }
}
public ICommand SelectColor1Command { get; }
public ICommand SelectColor2Command { get; }
public ICommand ApplyColorsCommand { get; }
public MainWindowViewModel()
{
SelectColor1Command = new RelayCommand(_ => SelectColor(1));
SelectColor2Command = new RelayCommand(_ => SelectColor(2));
ApplyColorsCommand = new RelayCommand(_ => ApplyColorsTo3DObject());
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void SelectColor(int colorNumber)
{
var colorDialog = new Autodesk.AutoCAD.Windows.ColorDialog();
if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (colorNumber == 1)
{
_color1 = colorDialog.Color;
SelectedColor1 = $"First 1: {_color1.ColorIndex}";
}
else
{
_color2 = colorDialog.Color;
SelectedColor2 = $"Second 2: {_color2.ColorIndex}";
}
}
}
private void ApplyColorsTo3DObject()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
if (_color1 == null || _color2 == null)
{
ed.WriteMessage("\nPlease chose 2 color.");
return;
}
using (var lck = doc.LockDocument())
{
PromptEntityOptions peo = new PromptEntityOptions("\nSelect 3D object:");
peo.SetRejectMessage("\nNot Solid3d.");
peo.AddAllowedClass(typeof(Solid3d), true);
var per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
ed.WriteMessage("\nUnfound object.");
return;
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
var solid3d = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Solid3d;
if (solid3d == null)
{
ed.WriteMessage("\nobject not Solid3d.");
return;
}
try
{
using (Brep brep = new Brep(solid3d))
{
int faceIndex = 0;
foreach (var face in brep.Faces)
{
try
{
var colorToApply = (faceIndex % 2 == 0) ? _color1 : _color2;
ApplyColorToFace(face, colorToApply);
faceIndex++;
}
catch (System.Exception ex)
{
ed.WriteMessage($"\nERROR: {ex.Message}");
}
}
}
ed.WriteMessage("\nSucceed .");
}
catch (System.Exception ex)
{
ed.WriteMessage($"\nError in Solid3d: {ex.Message}");
}
tr.Commit();
}
}
}
private void ApplyColorToFace(Autodesk.AutoCAD.BoundaryRepresentation.Face face, Autodesk.AutoCAD.Colors.Color color)
{
if (face != null)
{
try
{
var subEntityPath = face.SubentityPath;
var entityId = subEntityPath.GetObjectIds().FirstOrDefault();
if (entityId.IsValid)
{
using (Transaction tr = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
{
Solid3d solid = tr.GetObject(entityId, OpenMode.ForWrite) as Solid3d;
ObjectId[] ids = new ObjectId[] { entityId };
FullSubentityPath path = new FullSubentityPath(ids, new SubentityId(SubentityType.Null, IntPtr.Zero));
List SubentIds = new List();
using(Autodesk.AutoCAD.BoundaryRepresentation.Brep brep = new Autodesk.AutoCAD.BoundaryRepresentation.Brep(path))
{
foreach (Autodesk.AutoCAD.BoundaryRepresentation.Edge edge in brep.Edges)
{
SubentIds.Add(edge.SubentityPath.SubentId);
}
}
foreach (SubentityId subentId in SubentIds)
{
var entity = tr.GetObject(entityId, OpenMode.ForWrite) as Entity;
if (entity != null)
{
entity.Color = color;
}
tr.Commit();
}
}
}
}
catch (System.Exception ex)
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\nError in face: {ex.Message}");
}
}
}
public class RelayCommand : ICommand
{
private readonly Action _execute;
private readonly Predicate _canExecute;
public RelayCommand(Action execute, Predicate canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public bool CanExecute(object parameter) => _canExecute?.Invoke(parameter) ?? true;
public void Execute(object parameter) => _execute(parameter);
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
}
}
}
Я пытаюсь написать код по-другому и отлаживать код, но все равно
Подробнее здесь:
https://stackoverflow.com/questions/792 ... -objectarx
1736760897
Anonymous
когда я запускаю свой код, у меня всегда есть эта строка. Было выдано исключение типа Autodesk.AutoCAD.BoundaryRepresentation.Exception. я использую .netFramework 4.8 в Visual Studio 2022, есть ли у кого-нибудь такая же ошибка или кто-нибудь знает, как ее исправить? это вот код [code]using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.BoundaryRepresentation; using Autodesk.AutoCAD.Colors; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Windows.Input; namespace _3Dinterface.ViewModels { public class MainWindowViewModel : INotifyPropertyChanged { private string _selectedColor1; private string _selectedColor2; private Autodesk.AutoCAD.Colors.Color _color1; private Autodesk.AutoCAD.Colors.Color _color2; public string SelectedColor1 { get => _selectedColor1; set { _selectedColor1 = value; OnPropertyChanged(nameof(SelectedColor1)); } } public string SelectedColor2 { get => _selectedColor2; set { _selectedColor2 = value; OnPropertyChanged(nameof(SelectedColor2)); } } public ICommand SelectColor1Command { get; } public ICommand SelectColor2Command { get; } public ICommand ApplyColorsCommand { get; } public MainWindowViewModel() { SelectColor1Command = new RelayCommand(_ => SelectColor(1)); SelectColor2Command = new RelayCommand(_ => SelectColor(2)); ApplyColorsCommand = new RelayCommand(_ => ApplyColorsTo3DObject()); } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private void SelectColor(int colorNumber) { var colorDialog = new Autodesk.AutoCAD.Windows.ColorDialog(); if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (colorNumber == 1) { _color1 = colorDialog.Color; SelectedColor1 = $"First 1: {_color1.ColorIndex}"; } else { _color2 = colorDialog.Color; SelectedColor2 = $"Second 2: {_color2.ColorIndex}"; } } } private void ApplyColorsTo3DObject() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; if (_color1 == null || _color2 == null) { ed.WriteMessage("\nPlease chose 2 color."); return; } using (var lck = doc.LockDocument()) { PromptEntityOptions peo = new PromptEntityOptions("\nSelect 3D object:"); peo.SetRejectMessage("\nNot Solid3d."); peo.AddAllowedClass(typeof(Solid3d), true); var per = ed.GetEntity(peo); if (per.Status != PromptStatus.OK) { ed.WriteMessage("\nUnfound object."); return; } using (Transaction tr = db.TransactionManager.StartTransaction()) { var solid3d = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Solid3d; if (solid3d == null) { ed.WriteMessage("\nobject not Solid3d."); return; } try { using (Brep brep = new Brep(solid3d)) { int faceIndex = 0; foreach (var face in brep.Faces) { try { var colorToApply = (faceIndex % 2 == 0) ? _color1 : _color2; ApplyColorToFace(face, colorToApply); faceIndex++; } catch (System.Exception ex) { ed.WriteMessage($"\nERROR: {ex.Message}"); } } } ed.WriteMessage("\nSucceed ."); } catch (System.Exception ex) { ed.WriteMessage($"\nError in Solid3d: {ex.Message}"); } tr.Commit(); } } } private void ApplyColorToFace(Autodesk.AutoCAD.BoundaryRepresentation.Face face, Autodesk.AutoCAD.Colors.Color color) { if (face != null) { try { var subEntityPath = face.SubentityPath; var entityId = subEntityPath.GetObjectIds().FirstOrDefault(); if (entityId.IsValid) { using (Transaction tr = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction()) { Solid3d solid = tr.GetObject(entityId, OpenMode.ForWrite) as Solid3d; ObjectId[] ids = new ObjectId[] { entityId }; FullSubentityPath path = new FullSubentityPath(ids, new SubentityId(SubentityType.Null, IntPtr.Zero)); List SubentIds = new List(); using(Autodesk.AutoCAD.BoundaryRepresentation.Brep brep = new Autodesk.AutoCAD.BoundaryRepresentation.Brep(path)) { foreach (Autodesk.AutoCAD.BoundaryRepresentation.Edge edge in brep.Edges) { SubentIds.Add(edge.SubentityPath.SubentId); } } foreach (SubentityId subentId in SubentIds) { var entity = tr.GetObject(entityId, OpenMode.ForWrite) as Entity; if (entity != null) { entity.Color = color; } tr.Commit(); } } } } catch (System.Exception ex) { Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\nError in face: {ex.Message}"); } } } public class RelayCommand : ICommand { private readonly Action _execute; private readonly Predicate _canExecute; public RelayCommand(Action execute, Predicate canExecute = null) { _execute = execute ?? throw new ArgumentNullException(nameof(execute)); _canExecute = canExecute; } public bool CanExecute(object parameter) => _canExecute?.Invoke(parameter) ?? true; public void Execute(object parameter) => _execute(parameter); public event EventHandler CanExecuteChanged { add => CommandManager.RequerySuggested += value; remove => CommandManager.RequerySuggested -= value; } } } } [/code] Я пытаюсь написать код по-другому и отлаживать код, но все равно Подробнее здесь: [url]https://stackoverflow.com/questions/79243114/how-to-fix-exeption-type-of-in-c-net-objectarx[/url]