Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
5.6 kB
2
Indexable
Never
    [CommandMethod("CBD")]
    public void CBD()
    {
        try
        {
            using (Transaction trCad = Manager.DocData.TransactionManager.StartTransaction())
            {
                // Solicitar ao usuário que selecione linhas e arcos
                PromptSelectionOptions selOpts = new PromptSelectionOptions();
                selOpts.MessageForAdding = "Selecione linhas e arcos para formar os limites: ";
                PromptSelectionResult selRes = Manager.DocEditor.GetSelection(selOpts);

                if (selRes.Status != PromptStatus.OK)
                {
                    Manager.DocEditor.WriteMessage("\nSeleção cancelada.");
                    return;
                }

                SelectionSet selSet = selRes.Value;
                List<Autodesk.AutoCAD.DatabaseServices.Curve> curves = new List<Autodesk.AutoCAD.DatabaseServices.Curve>();

                // Adiciona as curvas selecionadas à lista de curvas
                foreach (SelectedObject selObj in selSet)
                {
                    if (selObj != null)
                    {
                        Autodesk.AutoCAD.DatabaseServices.Entity ent = trCad.GetObject(selObj.ObjectId, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Entity;
                        if (ent is Autodesk.AutoCAD.DatabaseServices.Curve curve)
                        {
                            curves.Add(curve);
                        }
                    }
                }

                if (curves.Count == 0)
                {
                    Manager.DocEditor.WriteMessage("\nNenhum objeto válido selecionado.");
                    return;
                }

                // Criação de Boundaries
                List<Polyline> polylines = new List<Polyline>();
                foreach (Autodesk.AutoCAD.DatabaseServices.Curve curve in curves)
                {
                    Point3dCollection fencePoints = new Point3dCollection { curve.StartPoint, curve.EndPoint };

                    // Seleciona objetos com base na cerca gerada pelas curvas
                    SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "LINE,ARC") });
                    PromptSelectionResult selection = Manager.DocEditor.SelectFence(fencePoints, filter);

                    if (selection.Status == PromptStatus.OK)
                    {
                        foreach (ObjectId objId in selection.Value.GetObjectIds())
                        {
                            using (Autodesk.AutoCAD.DatabaseServices.Curve selectedCurve = objId.GetObject(OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Curve)
                            {
                                if (selectedCurve != null)
                                {
                                    // Calcular bounds e tentar criar a boundary
                                    double maxY = double.MinValue;
                                    double minY = double.MaxValue;
                                    CalculateYBounds(selectedCurve, ref maxY, ref minY);

                                    // Trace a boundary
                                    using (DBObjectCollection boundaryObjects = Manager.DocEditor.TraceBoundary(new Point3d((selectedCurve.StartPoint.X + selectedCurve.EndPoint.X) / 2, (minY + maxY) / 2, 0.0), false))
                                    {
                                        if (boundaryObjects.Count == 1)
                                        {
                                            Polyline polyline = boundaryObjects[0] as Polyline;
                                            if (polyline != null)
                                            {
                                                polylines.Add(polyline);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Adicionar as polylines ao modelo
                if (polylines.Count > 0)
                {
                    BlockTable blockTable = (BlockTable)trCad.GetObject(Manager.DocData.BlockTableId, OpenMode.ForRead);
                    BlockTableRecord modelSpace = (BlockTableRecord)trCad.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                    foreach (Polyline polyline in polylines)
                    {
                        modelSpace.AppendEntity(polyline);
                        trCad.AddNewlyCreatedDBObject(polyline, true);
                    }

                    Manager.DocEditor.WriteMessage("\nLimites criados com sucesso.");
                }

                trCad.Commit();
            }
        }
        catch (Autodesk.AutoCAD.Runtime.Exception ex)
        {
            Manager.DocEditor.WriteMessage("\nErro: " + ex.Message);
        }
    }

    // Função para calcular os bounds Y de uma curva
    private void CalculateYBounds(Autodesk.AutoCAD.DatabaseServices.Curve curve, ref double maxY, ref double minY)
    {
        // Atualiza maxY e minY com base na geometria da curva
        Point3d start = curve.StartPoint;
        Point3d end = curve.EndPoint;

        if (start.Y > maxY) maxY = start.Y;
        if (start.Y < minY) minY = start.Y;
        if (end.Y > maxY) maxY = end.Y;
        if (end.Y < minY) minY = end.Y;
    }
}
Leave a Comment