Label Depth to Invert

Label Depth to Invert

sneha.sadaphal
Advocate Advocate
1,126 Views
11 Replies
Message 1 of 12

Label Depth to Invert

sneha.sadaphal
Advocate
Advocate

Hi,

I am creating depth to invert band by selecting the profile 

but when I run the command first time then the label is not getting created. but running it second time then it is getting creating.
don't have any idea why this is happening.

here is image for depth to invert band label which is placed after executing the command second time.

snehasadaphal_0-1679575540177.png

here is code 

snehasadaphal_1-1679575576152.png

 

Is any one having the solution for this?

0 Likes
1,127 Views
11 Replies
Replies (11)
Message 2 of 12

Jeff_M
Consultant
Consultant
More code is needed, preferably as actual code not a screenshot. Something that can easily be run & tested is best.
Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 3 of 12

varshaauti27
Advocate
Advocate

Try 

editor.Regen();

and see if it works. 

0 Likes
Message 4 of 12

sneha.sadaphal
Advocate
Advocate

Hi @Jeff_M Please find attached detail code for inserting depth to invert band

0 Likes
Message 5 of 12

sneha.sadaphal
Advocate
Advocate

Hi @varshaauti27 I tried with editor.regen(). but it is still showing empty band.

0 Likes
Message 6 of 12

hippe013
Advisor
Advisor

In my experience, sometimes ed.Regen() does not work as expected where using sendStringToExecute does work. 

 

 

aDoc.SendStringToExecute("REGEN ", True, False, False)

 

(vb.net)

 

Note, that this should be placed AFTER you commit your transaction. 

0 Likes
Message 7 of 12

Jeff_M
Consultant
Consultant

Thanks for the code @sneha.sadaphal It still needed some additional code to run so I moved things around to how I would do it. I agree with @hippe013 that a regen needs to be done after the transaction is committed. However, rather than calling for a full editor regen, I use the internal RegenEntity method. Here is my updated code which functions as expected.

        [CommandMethod("InsertDepthToInvertBand")]
        public static void InsertDTIInvertBand()
        {
            CivilDocument civDoc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument;
            Database db = Application.DocumentManager.MdiActiveDocument.Database;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            ProfileView profileView = null;
            #region For Inserting Band
            //Insert bandstyle with chainage values by comparing created profile and existing profile
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                try
                {
                    var entOpts = new PromptEntityOptions("\nSelect ProfileView:");
                    entOpts.SetRejectMessage("..not a ProfileView, try again.");
                    entOpts.AddAllowedClass(typeof(ProfileView), true);
                    var entRes = ed.GetEntity(entOpts);
                    if (entRes.Status != PromptStatus.OK) return;
                    var pvId = entRes.ObjectId;
                    profileView = (ProfileView)pvId.GetObject(OpenMode.ForWrite);
                    var collection = profileView.Bands.GetBottomBandItems();
                    BandStyleCollection bandStyleCollection = civDoc.Styles.BandStyles.ProfileViewProfileDataBandStyles;
                    if (!bandStyleCollection.Contains("Depth To Invert"))
                        return;
                    var bandStyle = (BandStyle)bandStyleCollection["Depth To Invert"].GetObject(OpenMode.ForRead);
  
                    entOpts.Message = "\nSelect Surface Profile:";
                    entOpts.SetRejectMessage("..not a Profile, try again.");
                    entOpts.AddAllowedClass(typeof(Profile), true);
                    entRes = ed.GetEntity(entOpts);
                    if (entRes.Status != PromptStatus.OK) return;
                    var prof1id = entRes.ObjectId;
                    var prof1 = (Profile) prof1id.GetObject(OpenMode.ForRead);
                    
                    entOpts.Message = "\nSelect Created Profile:";
                    entRes = ed.GetEntity(entOpts);
                    if (entRes.Status != PromptStatus.OK) return;
                    var prof2id = entRes.ObjectId;

                    double startstation = prof1.StartingStation;
                    double endstation = prof1.EndingStation;

                    collection.Add(bandStyle.Id);
                    collection.Last().Gap = 0.0;
                    collection.Last().ShowLabels = true;
                    collection.Last().MajorInterval = 100;
                    collection.Last().MinorInterval = 10;

                    //Thread.Sleep(2000);
                    collection.Last().Profile1Id = prof1id;
                    collection.Last().Profile2Id = prof2id;
                    profileView.Bands.SetBottomBandItems(collection);
                    tr.Commit();
                    Autodesk.AutoCAD.Internal.Utils.RegenEntity(pvId);
                }
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }
            #endregion
        }

 

Jeff_M, also a frequent Swamper
EESignature
Message 8 of 12

sneha.sadaphal
Advocate
Advocate

Hi @Jeff_M I tried using your code but still it is placing empty band.

0 Likes
Message 9 of 12

hippe013
Advisor
Advisor

@sneha.sadaphal 

The code that @Jeff_M posted works as intended. Are you able to post your drawing or an example drawing with the style in it? It seems to me that the issue is either in the data or in the style because the code inserts the band style onto the profile view as expected and it is visible when the command ends. 

Message 10 of 12

sneha.sadaphal
Advocate
Advocate

Hi @hippe013 Please see attached latest code and drawing.

0 Likes
Message 11 of 12

hippe013
Advisor
Advisor

It does not appear that your drawing has a band style that is called 'Depth To Invert'. So what happens in your code when the drawing does not contain the style 'Depth To Invert'? Nothing. Nothing happens. The user is not given any feedback. I would suggest implementing a function along the lines of GetOrCreateDepthToInvertStyle or at the very least provide the user with some feedback if the style does not exist. 

 

I would also suggest that you look into PromptEntityOptions. That way you can get rid of your while loops and if statements for when you select an entity. See example: 

(vb.net)

Dim opt As New PromptEntityOptions(vbCrLf & "Select Profile View:")
opt.SetRejectMessage(vbCrLf & "Selected entity must be a ProfileView. Try again.")
opt.AddAllowedClass(GetType(ProfileView), True)

Dim result As PromptEntityResult = ed.GetEntity(opt)

If result.Status <> PromptStatus.OK Then Exit Sub

 

This way the object selected is guaranteed to be of the type that you specify in AddAllowedClass. 

 

In my opinion your code needs to be refactored. You are opening objects for write when you are only reading it. It appears that you are creating additional profiles when you code is supposed to be just simply adding a band to a profile view. I suggest rewriting your code and only perform the action that you originally intended. It is difficult for those trying to help as it is like trying to hit a moving target. 

0 Likes
Message 12 of 12

sneha.sadaphal
Advocate
Advocate

Thanks @hippe013 for your suggestions. I will try to rewrite my code accordingly. 

0 Likes