Get Properties Upon Selection

Get Properties Upon Selection

DirtyDanLand
Enthusiast Enthusiast
1,752 Views
13 Replies
Message 1 of 14

Get Properties Upon Selection

DirtyDanLand
Enthusiast
Enthusiast

I have created my own tool palette.  When that tool palette is visible I want to populate it with information from selecting a pline.

 

I have it somewhat working but not exactly the way that I would like it to..

 

Currently, I click on pline and I am prompted to select an object. I then select the pline and I have to hit enter in order for a text box on the palette to receive the layer of the pline.

 

How can I make it so I only have the select the pline just once for the info to show?

 

Here is my current code located within the palette code...

 

        void doc_ImpliedSelectionChanged(object sender, EventArgs e)
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            {
                Transaction tr = db.TransactionManager.StartTransaction();
                using (tr)
                {
                    PromptSelectionResult acPSR = ed.GetSelection();
                    if (acPSR.Value.Count == 1 && acPSR.Status == PromptStatus.OK)
                    {
                        SelectionSet acSSet = acPSR.Value;
                        foreach (SelectedObject acSObj in acSSet)
                        {
                            Polyline pl = (Polyline)tr.GetObject(acSObj.ObjectId, OpenMode.ForRead);
                            if (pl != null)
                            {
                                //ed.WriteMessage(pl.Layer + "\n");
                                textBox1.Text = pl.Layer;
                            }
                        }
                    }
                    // Committing is cheaper than aborting
                    tr.Commit();
                }
            }
        }//void doc_ImpliedSelectionChanged

 

Thanks

0 Likes
Accepted solutions (1)
1,753 Views
13 Replies
Replies (13)
Message 2 of 14

Keith.Brown
Advisor
Advisor

When the implied selection changes, instead of asking the user for a selection, just get the implied selection.

 

editor.SelectImplied()

This will give you all of the items in the current selection set.  Make sure you check for null because the event will fire if the user deselects everything.  The order of the items in the selection is the same as the order that the user selected them.

0 Likes
Message 3 of 14

DirtyDanLand
Enthusiast
Enthusiast

where in my code do I put

 

editor.SelectImplied()

 

Thanks

0 Likes
Message 4 of 14

DirtyDanLand
Enthusiast
Enthusiast

If I update the code per the following using the ed.SelectImplied the code functions as it should for the first selection but the next selection never updates the textbox

 

        void doc_ImpliedSelectionChanged(object sender, EventArgs e)
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            {
                Transaction tr = db.TransactionManager.StartTransaction();
                using (tr)
                {
                    PromptSelectionResult acPSR = ed.SelectImplied();
                    //PromptSelectionResult acPSR = ed.GetSelection();
                    if (acPSR.Value.Count == 1 && acPSR.Status == PromptStatus.OK)
                    {
                        SelectionSet acSSet = acPSR.Value;
                        foreach (SelectedObject acSObj in acSSet)
                        {
                            Polyline pl = (Polyline)tr.GetObject(acSObj.ObjectId, OpenMode.ForRead);
                            if (pl != null)
                            {
                                //ed.WriteMessage(pl.Layer + "\n");
                                textBox1.Text = pl.Layer;
                            }
                        }
                    }
                    // Committing is cheaper than aborting
                    tr.Commit();
                }
            }
        }//void doc_ImpliedSelectionChanged
0 Likes
Message 5 of 14

Keith.Brown
Advisor
Advisor

You fail to mention if you have more than 1 item selected when you get the next selection.

 

Per your code, the selection set can only have 1 item in it.  You will need to decide what to do if you have multiple items.  If you remove the count = 1 restriction then the way it is written now, it will cycle through each item and if it is a polyline then display the layer in a textbox.  If  there are multiple polylines then it will cycle through each of them so fast that you will only notice that the last item is written to the text box.  You should capture the layer of each polyline and if it is the same then display the layer.  If they are not the same then display something like "** Varies **".  That is a design decision that you will have to decide upon.

0 Likes
Message 6 of 14

norman.yuan
Mentor
Mentor

While Keith has offerred good suggestions, I can show some code I recently used in a similar requirement in one of my programming projects. The requirement is to show the selected entities in current drawing (when user clicks entity to select/highlight, or Shift+click to unselect/unhighlight). The selection changes shows in a small floating form (modeless form).

 

I though it would be better to show a complete, run-able project, rather than partial code, in my blog, so it would be much easier to read to code. Here is it:

 

http://drive-cad-with-code.blogspot.ca/2016/08/showing-current-selection-in-modeless.html

 

For your need, you only need to apply some kind of filtering mechanism before sending the selected ObjectId array to the form to show.

 

HTH

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 7 of 14

DirtyDanLand
Enthusiast
Enthusiast

Thanks Keith and Norman for your input...

 

The intention is to only be able to select a single object.

 

I have a higher priority project that i was just given so I have to put this on hold, I will report back when I get a chance to try your suggestions...

 

Thanks again

0 Likes
Message 8 of 14

DirtyDanLand
Enthusiast
Enthusiast

Keith

I verified that I only have one object selected with my code above, the textbox does not get updated with the layer of the pline.  The problem is the code is not executing when I select a different object after clearing current selection.  Im looking into why it does that.

 

 

Norman

Do you have an code examples using a dockable tool palette?

 

 

Thanks

Dan

0 Likes
Message 9 of 14

norman.yuan
Mentor
Mentor
Accepted solution

Well, again, in order to be able to present a relatively complete, run-able code, I just added extra code to the same post in my blog to show how to use a PaletteSet to show a selected single polyline information. You can easily modify the code to fit your need.

 

See here:

 

http://drive-cad-with-code.blogspot.ca/2016/08/showing-current-selection-in-modeless.html

 

HTH

Norman Yuan

Drive CAD With Code

EESignature

Message 10 of 14

DirtyDanLand
Enthusiast
Enthusiast
Thank you Norman.... I will report back once I get a chance to play with this...
0 Likes
Message 11 of 14

DirtyDanLand
Enthusiast
Enthusiast

Thanks Norman... works wonderfully...

0 Likes
Message 12 of 14

DirtyDanLand
Enthusiast
Enthusiast

Norman, I have another question for you...

 

I am changing the code that you created so the palette does not hide if a polyline is not selected.  The palette shows when the used enters a commmand to show it.  The used can select a desired polyline and information will be displayed on the palette. If the user selects some entity other than a polyline, no information is displayed on the palette and any info that was previously displayed is cleared.

 

I commented out 

_ps.Visible = false;

 

so the palette does not disapear.

 

next

 

I modified the following

 

Style = PaletteSetStyles.UsePaletteNameAsTitleForSingle;

to this

 

 

            Style = PaletteSetStyles.UsePaletteNameAsTitleForSingle |
                PaletteSetStyles.ShowPropertiesMenu |
                PaletteSetStyles.ShowAutoHideButton |
                PaletteSetStyles.ShowCloseButton;

so the close button and autohide button are shown but it does not seem to be working. Any ideas as to why???

 

 

Thanks

Dan

 

 

0 Likes
Message 13 of 14

norman.yuan
Mentor
Mentor

My guess is that by default, AutoCAD remembers PaletteSet style/position running in previous session. Since AutoCAD identify PaletteSet by its GUID id, you can use VS to generate a new GUID to replace the one currently in the PaleteSet's constructor, so that when you run the PaletteSet next time, AutoCAD will treat this PaletteSet as entirely new one, and its style/size/location... will then be honored by AutoCAD.

Norman Yuan

Drive CAD With Code

EESignature

Message 14 of 14

DirtyDanLand
Enthusiast
Enthusiast

that did it...

I could have sworn that I tried that yesterday...

 

thanks again

0 Likes