Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.5 kB
4
Indexable
Never
        [CommandMethod("CR_Grade")]
        public void CR_Grade()
        {
            // Selecionar ProfileViews existentes no desenho
            TypedValue[] filtro = new TypedValue[] { new TypedValue((int)DxfCode.Start, "AECC_PROFILE_VIEW") };
            SelectionFilter selFiltro = new SelectionFilter(filtro);
            PromptSelectionResult SelRes = Manager.DocEditor.GetSelection(selFiltro);

            if (SelRes.Status != PromptStatus.OK)
            {
                Manager.DocEditor.WriteMessage("\nNenhum ProfileView selecionado.");
                return;
            }

            F_CR_Grade Janela = new F_CR_Grade();
            Janela.ShowDialog();
            if (!Janela.Fazer)
            {
                return;
            }

            ObjectId IdEstilo = Manager.DocCivil.Styles.ProfileStyles[Janela.NomeEstiloPerfil];
            ObjectId IdLabel = Manager.DocCivil.Styles.LabelSetStyles.ProfileLabelSetStyles[Janela.NomeLabelPerfil];

            using (DocumentLock Lock = Manager.DocCad.LockDocument())
            {
                using (Transaction TrCad = Manager.DocCad.TransactionManager.StartTransaction())
                {
                    LayerTable TabelaLayers = (LayerTable)TrCad.GetObject(Manager.DocData.LayerTableId, OpenMode.ForRead);
                    ObjectId IdLayer = TabelaLayers[Janela.NomeLayerPerfil];
                    try
                    {
                        foreach (ObjectId profileViewId in SelRes.Value.GetObjectIds())
                        {
                            ProfileView profileView = TrCad.GetObject(profileViewId, OpenMode.ForRead) as ProfileView;
                            if (profileView == null) continue;

                            foreach (ObjectId profileId in profileView.GetProfileIds())
                            {
                                Profile existingProfile = TrCad.GetObject(profileId, OpenMode.ForRead) as Profile;
                                if (existingProfile == null || existingProfile.StyleType != ProfileStyleType.ExistingGround) continue;

                                string profileName = $"{Janela.PrefixoPerfil}_{profileView.Name}";
                                Profile newProfile = CreateProfile(TrCad, profileView.AlignmentId, profileName, IdLayer, IdEstilo, IdLabel);
                                AdjustProfileElevations(TrCad, newProfile, existingProfile);
                                Manager.DocEditor.WriteMessage($"\nPerfil de projeto '{profileName}' criado para o ProfileView '{profileView.Name}'.");
                            }
                        }
                        TrCad.Commit();
                    }
                    catch (System.Exception ex)
                    {
                        Manager.DocEditor.WriteMessage($"\nErro: {ex.Message}");
                    }
                }
            }
        }

        private Profile CreateProfile(Transaction TrCad, ObjectId alignmentId, string profileName, ObjectId IDLayer, ObjectId IdEstilo, ObjectId IdLabel)
        {
            Alignment alignment = TrCad.GetObject(alignmentId, OpenMode.ForRead) as Alignment;
            if (alignment == null) throw new System.Exception("Alinhamento não encontrado.");

            ObjectId profileId = Profile.CreateByLayout(profileName, alignmentId, IDLayer, IdEstilo, IdLabel);
            Profile newProfile = TrCad.GetObject(profileId, OpenMode.ForWrite) as Profile;
            return newProfile;
        }

        private void AdjustProfileElevations(Transaction tr, Profile profile, Profile existingProfile)
        {
            double startStation = existingProfile.StartingStation;
            double endStation = existingProfile.EndingStation;
            double startElevation = existingProfile.ElevationAt(startStation);
            double endElevation = existingProfile.ElevationAt(endStation);

            profile.UpgradeOpen();
            ProfilePVICollection pviCollection = profile.PVIs;
            if (pviCollection.Count > 0)
            {
                pviCollection[0].Elevation = startElevation;
                pviCollection[pviCollection.Count - 1].Elevation = endElevation;
            }
            else
            {
                profile.PVIs.AddPVI(startStation, startElevation);
                profile.PVIs.AddPVI(endStation, endElevation);
            }
            profile.DowngradeOpen();
        }
    }
}
Leave a Comment