Untitled
Código Formulario using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.Civil.DatabaseServices; using Autodesk.Civil.DatabaseServices.Styles; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace GF_APPInfra.Formularios.Acabamentos { public partial class F_IN_FeatureLine : Form { public string NomeLabel { get { return CB_Label.SelectedItem.ToString(); } } public bool SentPositivo { get { return CKB_Positivo.Checked; } } public bool SentNegativo { get { return CKB_Negativo.Checked; } } public bool Fazer { get; set; } public F_IN_FeatureLine() { InitializeComponent(); } private void F_IN_FeatureLine_Load(object sender, EventArgs e) { using (DocumentLock Lock = Manager.DocCad.LockDocument()) { using (Transaction TrCad = Manager.DocCad.TransactionManager.StartTransaction()) { // Lista para armazenar os nomes dos Labels List<string> labelNames = new List<string>(); foreach (ObjectId IdLabel in Manager.DocCivil.Styles.LabelStyles.GeneralLineLabelStyles) { GeneralSegmentLabel Label = TrCad.GetObject(IdLabel, OpenMode.ForRead) as GeneralSegmentLabel; labelNames.Add(Label.Name); } // Ordena os nomes dos Labels labelNames.Sort(); // Adiciona os nomes dos Labels ao ComboBox foreach (string labelName in labelNames) { CB_Label.Items.Add(labelName); } CB_Label.SelectedIndex = 0; TrCad.Commit(); } } } private void BT_Cancelar_Click(object sender, EventArgs e) { Fazer = false; Close(); } private void BT_OK_Click(object sender, EventArgs e) { Fazer = true; Close(); } } } Código Principal [CommandMethod("IN_FeatureLine")] public void IN_FeatureLine() { PromptSelectionOptions selectionOptions = new PromptSelectionOptions { MessageForAdding = "Selecione as feature lines" }; // Filtro para selecionar apenas Feature Lines SelectionFilter selectionFilter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "AECC_FEATURE_LINE") }); // Solicitar ao usuário que selecione as feature lines PromptSelectionResult selectionResult = Manager.DocEditor.GetSelection(selectionOptions, selectionFilter); // Verificar se a seleção foi bem-sucedida if (selectionResult.Status != PromptStatus.OK) return; F_IN_FeatureLine Janela = new F_IN_FeatureLine(); Janela.ShowDialog(); if (!Janela.Fazer) { return; } using (Transaction transaction = Manager.DocCad.TransactionManager.StartTransaction()) { BlockTable blockTable = (BlockTable)transaction.GetObject(Manager.DocData.BlockTableId, OpenMode.ForRead); BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite); ObjectId[] objectIds = selectionResult.Value.GetObjectIds(); foreach (ObjectId objectId in objectIds) { FeatureLine featureLine = (FeatureLine)transaction.GetObject(objectId, OpenMode.ForWrite); bool precisaInverter = false; // Verifica o sentido selecionado if (Janela.SentPositivo && featureLine.EndPoint.Z < featureLine.StartPoint.Z) { precisaInverter = true; } else if (Janela.SentNegativo && featureLine.EndPoint.Z > featureLine.StartPoint.Z) { precisaInverter = true; } if (precisaInverter) { // Captura os pontos da feature line Point3dCollection points = featureLine.GetPoints(FeatureLinePointType.AllPoints); // Cria uma nova polyline invertida Polyline3d polyline3d = new Polyline3d(); blockTableRecord.AppendEntity(polyline3d); transaction.AddNewlyCreatedDBObject(polyline3d, true); for (int i = points.Count - 1; i >= 0; i--) { PolylineVertex3d vertex = new PolylineVertex3d(points[i]); polyline3d.AppendVertex(vertex); transaction.AddNewlyCreatedDBObject(vertex, true); } // Cria uma nova Feature Line baseada na polyline invertida ObjectId newFeatureLineId = FeatureLine.Create(featureLine.Name, polyline3d.ObjectId); FeatureLine newFeatureLine = (FeatureLine)transaction.GetObject(newFeatureLineId, OpenMode.ForWrite); // Copia o estilo e camada da original newFeatureLine.StyleId = featureLine.StyleId; newFeatureLine.LayerId = featureLine.LayerId; // Troca os IDs das Feature Lines para manter a nova linha no lugar da original newFeatureLine.SwapIdWith(featureLine.ObjectId, true, true); // Apaga a polyline e a linha original polyline3d.Erase(true); featureLine.Erase(true); featureLine = newFeatureLine; } // Aplica o Label escolhido pelo usuário GeneralSegmentLabel newLabel = new GeneralSegmentLabel(); newLabel.StyleId = Manager.DocCivil.Styles.LabelStyles.GeneralLineLabelStyles.Cast<ObjectId>() .First(id => transaction.GetObject(id, OpenMode.ForRead) is GeneralSegmentLabel lbl && lbl.Name == Janela.NomeLabel); // Aqui você pode adicionar o Label à feature line ou manipular de acordo com sua necessidade específica. } transaction.Commit(); } }
Leave a Comment