Author Topic: Command with selection?  (Read 5656 times)

0 Members and 1 Guest are viewing this topic.

Keith Brown

  • Swamp Rat
  • Posts: 601
Command with selection?
« on: September 26, 2012, 05:53:47 PM »
Can someone point me in the right direction to impliment a command with an entity/entities preselected?  I am looking to impliment a command that will work like the move command.  If one or more entities are selected then it will start to execute the command immediately, otherwise it will ask for the user to pick some entities.  Are there any examples in the Autocad install that shows this? or possibly a previous post that someone knows of.  I have searched but not quite sure of the keywords i need.
I would like to upgrade the  Attach a property set to an object post on the Developer blog posted today with a pickfirst selection set implimentation.  Eventually I would just like to automate the entire process so it always attached certain property sets to certain objects.  I just do not know where to begin.
Thanks in advance.
 
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

BlackBox

  • King Gator
  • Posts: 3770
"How we think determines what we do, and what we do determines what we get."

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Command with selection?
« Reply #2 on: September 27, 2012, 01:15:01 AM »
Hi,

IMO, there's unusefull code in the developper's guide sample.
While the command uses the 'UsePickSet' CommandFlag, there's no need use the Editor.SelectImplied() method.
IOW, the code would be exactly the same as for a 'post selection' command except the CommandFlag.
Speaking English as a French Frog

TheMaster

  • Guest
Re: Command with selection?
« Reply #3 on: September 27, 2012, 06:40:34 AM »
Hi,

IMO, there's unusefull code in the developper's guide sample.
While the command uses the 'UsePickSet' CommandFlag, there's no need use the Editor.SelectImplied() method.
IOW, the code would be exactly the same as for a 'post selection' command except the CommandFlag.

Agreed, and it's even worse than that, if you read the comments:

Quote
Obtain the PickFirst Selection Set
The PickFirst selection set is created when you select objects prior to starting a command. Several conditions must be present in order to obtain the objects of a PickFirst selection set, these conditions are:
  • PICKFIRST system variable must be set to 1.
  • UsePickSet command flag must be defined with the command that should use the Pickfirst selection set
  • Call the SelectImplied method to obtain the PickFirst selection set
The SetImpliedSelection method is used to clear the current PickFirst selection set.

The writer is flat-out wrong on two counts (redded above).  In addition to what you noted, the current PickFirst selection set is automatically cleared when a command ends, unless the command uses the CommandFlags.Redraw flag, and calls SetImpliedSelection() to set the current pickfirst selection that will remain after the command ends.

As I mentioned, one needs to be careful of these docs, because much of it was not technically-edited by anyone with the background or familiarity with the subject matter. When things go wrong to the extent we're seeing, one can't blame it on writers, it's their management that deserves most of the credit.
« Last Edit: September 27, 2012, 07:07:49 AM by TT »

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Command with selection?
« Reply #4 on: September 27, 2012, 07:34:28 AM »
If the select implied method is not to be used and the code should look the same as a post selection command how would you obtain the selection set.of any objects picked before the command is ran?



Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

BlackBox

  • King Gator
  • Posts: 3770
Re: Command with selection?
« Reply #5 on: September 27, 2012, 08:19:33 AM »
If the select implied method is not to be used and the code should look the same as a post selection command how would you obtain the selection set.of any objects picked before the command is ran?

If I am understanding what Gile, and Tony are saying correctly, then this should be what you're after:

While the command uses the 'UsePickSet' CommandFlag, there's no need use the Editor.SelectImplied() method.
IOW, the code would be exactly the same as for a 'post selection' command except the CommandFlag.

HTH
"How we think determines what we do, and what we do determines what we get."

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Command with selection?
« Reply #6 on: September 27, 2012, 08:20:21 AM »
If the select implied method is not to be used and the code should look the same as a post selection command how would you obtain the selection set.of any objects picked before the command is ran?

In your code, you would have a call to Editor.GetSelection(), possibly with a SelectionFilter, which will use the ImpliedSelection if it exists or prompt the user to select objects if not.

Here's a little sample which moves the selected entities 10 units on X axis:

Code - C#: [Select]
  1.         [CommandMethod("test", CommandFlags.Modal | CommandFlags.UsePickSet)]
  2.         public void Test()
  3.         {
  4.             Document doc = AcAp.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.             PromptSelectionResult psr = ed.GetSelection();
  8.             if (psr.Status != PromptStatus.OK) return;
  9.             using (Transaction tr = db.TransactionManager.StartTransaction())
  10.             {
  11.                 foreach (ObjectId id in psr.Value.GetObjectIds())
  12.                 {
  13.                     Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
  14.                     ent.TransformBy(Matrix3d.Displacement(Vector3d.XAxis * 10.0));;
  15.                 }
  16.                 tr.Commit();
  17.             }
  18.         }
« Last Edit: September 27, 2012, 08:25:51 AM by gile »
Speaking English as a French Frog

BlackBox

  • King Gator
  • Posts: 3770
Re: Command with selection?
« Reply #7 on: September 27, 2012, 08:31:16 AM »
At the risk of tempting fate, I was going to post a link to the Command Definition documentation page, and follow it up with another, non-Autodesk page here (by SpiderInNet1) to back it up, and noticed quite a difference between the listed CommandFlags.

Is this a version difference, or simply a lack of documentation by Autodesk (again)?
"How we think determines what we do, and what we do determines what we get."

kaefer

  • Guest
Re: Command with selection?
« Reply #8 on: September 27, 2012, 09:55:52 AM »
At the risk of tempting fate, I was going to post a link to the Command Definition documentation page, ...

To muddy the waters further, here's what arxmgd.chm ("ObjectARX for AutoCAD 2013: Managed Class Reference Guide") has to add:
Quote
UsePickSet = 2

When the pickfirst set is retrieved, it is cleared within AutoCAD.
Command is able to retrieve the pickfirst set via the functions ads_ssgetfirst() or ads_ssget("I.").
Command can set the pickfirst set via ads_sssetfirst(), but it only stays set until the command ends.
Command cannot retrieve nor set grips. 

On other people's wizards I tend to have no discernable opinion.

TheMaster

  • Guest
Re: Command with selection?
« Reply #9 on: September 27, 2012, 07:43:42 PM »
At the risk of tempting fate, I was going to post a link to the Command Definition documentation page, and follow it up with another, non-Autodesk page here (by SpiderInNet1) to back it up, and noticed quite a difference between the listed CommandFlags.

Is this a version difference, or simply a lack of documentation by Autodesk (again)?

First, the link from the Autodesk docs you posted also has an error:

UsePickSet - When the pickfirst set is retrieved, it is cleared.

That's not what it means. The pickfirst set is always cleared. It's clear that the writer and his managers/technical editor have no clue here, because it's the exact same subject that the other errors are about. UsePickSet means that when you request a selection from the user, using any means that ultimately leads to a call to acedSsGet(), the current pickfirst selection will be used. That's all it means.

Regarding the 'undocumented' flags, most are of little use to your apps and they're really intended for internal use only. For example, the InProgress flag might be set on the AcEdCommand class entry for a command, while that command is in progress, and used by code that searches through all AcEdCommands for whatever purpose.  So, I wouldn't worry too much about the undocumented flags.

Chumplybum

  • Newt
  • Posts: 97
Re: Command with selection?
« Reply #10 on: September 27, 2012, 11:49:17 PM »
+1 for just adding in the UsePickSet into the command method

Code: [Select]

'Autodesk
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime

Public Class Class1

    ''' <summary>
    ''' preselect with filter
    ''' </summary>
    ''' <remarks></remarks>
    <CommandMethod("PF1", CommandFlags.Modal + CommandFlags.UsePickSet)> _
    Public Shared Sub PICKFIRST1()

        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As database = doc.Database

        Dim result As PromptSelectionResult = ed.GetSelection()

        If result.Status = PromptStatus.OK Then
            ed.WriteMessage(String.Format("{0} entities selected", result.Value.Count.ToString))
        End If

    End Sub

    ''' <summary>
    ''' preselect with filter
    ''' </summary>
    ''' <remarks></remarks>
    <CommandMethod("PF2", CommandFlags.Modal + CommandFlags.UsePickSet)> _
    Public Shared Sub PICKFIRST2()

        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database

        Dim filter As New SelectionFilter(New TypedValue() {New TypedValue(DxfCode.Start, "TEXT")})
        Dim result As PromptSelectionResult = ed.GetSelection(filter)

        If result.Status = PromptStatus.OK Then
            ed.WriteMessage(String.Format("{0} entities selected", result.Value.Count.ToString))
        End If

    End Sub

    ''' <summary>
    ''' no preselect
    ''' </summary>
    ''' <remarks></remarks>
    <CommandMethod("PF3", CommandFlags.Modal)> _
    Public Shared Sub PICKFIRST3()

        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database

        Dim result As PromptSelectionResult = ed.GetSelection()

        If result.Status = PromptStatus.OK Then
            ed.WriteMessage(String.Format("{0} entities selected", result.Value.Count.ToString))
        End If

    End Sub

End Class


Jeff H

  • Needs a day job
  • Posts: 6150
Re: Command with selection?
« Reply #11 on: February 04, 2013, 08:46:58 PM »
Is SelectImplied and SetImpliedSelection used for situations where you want to modify existing entities selected, but  do not want to prompt the user to select entities and to retain current selection?


For example changing the current layer in layer drop-down box how the selection remains, and the selected entities layer change, but you are not prompted if nothing is selected.

TheMaster

  • Guest
Re: Command with selection?
« Reply #12 on: February 06, 2013, 08:12:08 PM »
Is SelectImplied and SetImpliedSelection used for situations where you want to modify existing entities selected, but  do not want to prompt the user to select entities and to retain current selection?


For example changing the current layer in layer drop-down box how the selection remains, and the selected entities layer change, but you are not prompted if nothing is selected.

SelectImplied is something one would only use if they wanted to obtain a pickfirst selection set, without automatically prompting the user to make a selection, if no pickfirst selection exists.

SelectImplied was changed in the API a few releases back, to not use acedSSGet() like it did previously, and that was done to accomodate the code used by the ribbon controls, to avoid changing the Previous selection set. Prior to that, SelectImplied used acedSSGet() which resulted in changing the Previous selection set.

So, SelectImplied is useful if you have controls that reflect something related to the current/pickfirst selection, and is what you could use to examine the Pickfirst selection without changing the Previous selection, in order to update your controls every time the pickfirst selection changes, just as various controls on the Ribbon do.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Command with selection?
« Reply #13 on: February 07, 2013, 01:45:39 AM »
Thanks Tony,


Just wish and do not understand why they can't produce documentation with a little insight and intent of use to help in learning the API.