See the top rated post in this thread. Click here

Results 1 to 4 of 4

Thread: Adding code generated entities to a selection set

  1. #1
    Member
    Join Date
    2004-06
    Posts
    13
    Login to Give a bone
    0

    Question Adding code generated entities to a selection set

    I am slowly trying to get to grips with VB.NET, after a good few years of messing around with VBA.
    I’m currently writing some code to try to improve CAD’s MEASURE command, but I’ve hit a bit of a stumbling block.
    I have my command working to the extent that it will place blocks at regular intervals along a polyline, but I would like to add the generated blocks to a selection set, so you can use “previous” to select them like you can with CAD’s MEASURE command.

    e.g. If I used the MEASURE command in CAD to place blocks along a polyline, then wanted to explode the blocks directly after, I could just use:
    EXPLODE <ENTER>
    P <ENTER>
    to explode the previous selection set, but my code never adds the blocks to a selection set – so you have to select them manually.

    To add an entity to a selection set in VBA you would do something like this (excuse the pseudo-code):

    Dim Ent as Entity
    Ent = (whatever)

    Dim SS as SelectionSet
    SS.Add(Ent)

    VB.NET doesn’t seem to have an Add method for selection sets though, as far as I can gather you can only get them by using SelectAll & a dxf code filter – which would just grab all instances of the block, unless I went down the route of adding a temporary unique layer for them, or something along those lines.

    Am I missing something obvious here?

    Thanks in advance,
    Will

  2. #2
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    1

    Default Re: Adding code generated entities to a selection set

    For somebody else to whom this would be interesting as well

    Take a look at SetImpliedSelection method of document Editor

    Here is Q&D example:
    (slightly tested on A2009)

    Code:
           static public void AddItemsExm()
            {
                Editor ed = Application.DocumentManager.MdiActiveDocument.Edit​or;
     
                Database db = HostApplicationServices.WorkingDatabase;
     
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    try
                    {
                        BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
     
                        SelectionFilter sf = new SelectionFilter(
     
                         new TypedValue[] { new TypedValue(0, "LINE") });
     
                        PromptSelectionOptions pso = new PromptSelectionOptions();
     
                        pso.MessageForAdding = "\nSelect lines only >>";
     
                        pso.SingleOnly = false;
     
                        PromptSelectionResult res = ed.GetSelection(pso, sf);
     
                        if (res.Status != PromptStatus.OK)
     
                            return;
     
                        SelectionSet main = res.Value;
     
                        ObjectId[] oids = main.GetObjectIds();
     
                        ed.WriteMessage("\nSelected {0} objects", oids.Length);
     
                        Line ln = new Line(new Point3d(0, 0, 0), new Point3d(0, 1000, 0));//dummy line for test
     
     
                        btr.AppendEntity(ln);
     
                        tr.AddNewlyCreatedDBObject(ln, true);
     
                        int cnt = oids.Length;
     
                        Array.Resize(ref oids, cnt + 1);
     
                        oids[cnt] = ln.ObjectId;
     
                        SelectionSet newset = SelectionSet.FromObjectIds(oids);
     
                        main = null;
     
                        ed.SetImpliedSelection(newset);
     
                        ed.WriteMessage("\nAfter adding line: {0} objects", newset.GetObjectIds().Length);
     
                        foreach (SelectedObject obj in newset)
                        {
                            Entity en = (Entity)tr.GetObject(obj.ObjectId, OpenMode.ForRead);
     
                            en.UpgradeOpen();
     
                            en.ColorIndex = 1;
     
                            en.DowngradeOpen();
     
                        }
     
                        tr.Commit();
     
                    }
     
                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        Autodesk.AutoCAD.ApplicationServices.Application.S​howAlertDialog(ex.Message);
                    }
                }
            }
    ~'J'~

  3. #3
    Member
    Join Date
    2004-06
    Posts
    13
    Login to Give a bone
    0

    Thumbs up Re: Adding code generated entities to a selection set

    Thanks Fixo,
    That more or less fixed my problem.
    I just needed to use "document.SendStringToExecute" to execute the SELECT command after SetImpliedSelection, in order to de-select the items and make them available as the previous selection set.
    Here's the (abridged) code I used in the end:

    Code:
    '####################################################
    
    <CommandMethod("MB", CommandFlags.Redraw)> _
    Public Sub NewMeasure()
         Dim myDWG As ApplicationServices.Document =  ApplicationServices.Application.DocumentManager.MdiActiveDocument
         Dim myEd As EditorInput.Editor = myDWG.Editor
    
    		
         Dim MeasureObjects As DBObjectcollection = CreateObjects 
    
    
         If MeasureObjects.Count > 0 Then
             Dim SelSet As SelectionSet = AddToSelectionSet(MeasureObjects)
             myEd.SetImpliedSelection(SelSet)
             myDWG.SendStringToExecute("SELECT ", False, False, False)
         End If
    End Sub
    
    Function AddToSelectionSet(ByVal Objects As DBObjectCollection) As SelectionSet
         Dim ObjectIDs(0 To (Objects.Count - 1)) As ObjectId
         Dim ThisObjectID As ObjectId
         Dim Obj As DBObject
         For i As Integer = 0 To (Objects.Count - 1)
             Obj = Objects(i)
             ThisObjectID = Obj.ObjectId
             ObjectIDs(i) = ThisObjectID
         Next
         Dim SelSet As SelectionSet = SelectionSet.FromObjectIds(ObjectIDs)
         Return SelSet
    End Function
    
    '####################################################
    Thanks,
    Will
    Last edited by RobertB; 2010-08-18 at 09:38 PM. Reason: Added [code] tags

  4. #4
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    0

    Default Re: Adding code generated entities to a selection set

    Quote Originally Posted by lambwill View Post
    Thanks Fixo,
    That more or less fixed my problem.

    Will
    You're welcome, Will
    Cheers
    Oleg

    ~'J'~

Similar Threads

  1. Replies: 20
    Last Post: 2015-03-16, 10:59 PM
  2. 3d view generated from selection and settings of current plan view
    By Wish List System in forum Revit Architecture - Wish List
    Replies: 2
    Last Post: 2014-11-14, 03:43 AM
  3. Create a Selection Set of Entities as They are Created
    By autocad.wishlist1734 in forum AutoCAD Wish List
    Replies: 0
    Last Post: 2008-10-12, 06:37 PM
  4. Replies: 1
    Last Post: 2007-03-08, 01:12 PM
  5. Slow selection of entities.
    By peter.woodcock in forum AutoCAD General
    Replies: 2
    Last Post: 2004-09-23, 01:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •