Author Topic: Problem with group code  (Read 1979 times)

0 Members and 1 Guest are viewing this topic.

SOURCECODE

  • Guest
Problem with group code
« on: November 29, 2011, 05:32:50 AM »
I have a code which asks the user to name a group and  to select entities using a selection window.
The Selection should then be appended to the group. But this does not work.
 I get an error in autocad saying
"Unhandled Exception has occured in a component in your application"
The error is cause by the following line
tr.AddNewlyCreatedDBObject(grp, True)


What changes should I make

Full Code is shown below.

Code: [Select]
Public Class Class2

<CommandMethod("CG")> _
Public Sub CreateGroup()
Dim mydwg As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = mydwg.Database
Dim myeditor As Editor = mydwg.Editor
Dim tr As Transaction = db.TransactionManager.StartTransaction()
Using tr
' Get the group dictionary from the drawing
Dim gd As DBDictionary = DirectCast(tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead), DBDictionary)
' Check the group name, to see whether it's
' already in use
Dim pso As New PromptStringOptions(vbLf & "Enter new group name: ")
pso.AllowSpaces = True
' A variable for the group's name
Dim grpName As String = ""
Do
Dim pr As PromptResult = myeditor.GetString(pso)
' Just return if the user cancelled
' (will abort the transaction as we drop out of the using
' statement's scope)
If pr.Status <> PromptStatus.OK Then
Return
End If
Try
' Validate the provided symbol table name
SymbolUtilityServices.ValidateSymbolName(pr.StringResult, False)
' Only set the block name if it isn't in use
If gd.Contains(pr.StringResult) Then
myeditor.WriteMessage(vbLf & "A group with this name already exists.")
Else
grpName = pr.StringResult
End If
Catch
 
' An exception has been thrown, indicating the
' name is invalid
myeditor.WriteMessage(vbLf & "Invalid group name.")
 
 
End Try
Loop While grpName = ""
' Create our new group...
 
 
Dim grp As New Group("Test group", True)
' Add the new group to the dictionary
 
 
gd.UpgradeOpen()
Dim grpId As ObjectId = Selectobjectsforgroup()
[color=red]tr.AddNewlyCreatedDBObject(grp, True)[/color]' Open the model-space
 
Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
 
Dim ms As BlockTableRecord = DirectCast(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
 
 
' Commit the transaction
tr.Commit()
 
End Using
End Sub
 
Private Function Selectobjectsforgroup() As ObjectId
Dim mydwg As Document = DocumentManager.MdiActiveDocument
Dim db As Database = mydwg.Database
Dim myeditor As Editor = DocumentManager.MdiActiveDocument.Editor
Dim myPPR As Point3d = myeditor.GetPoint("Select 1st Point: ").Value
Dim myPPR1 As Point3d = myeditor.GetCorner("Select 2nd Point: ", myPPR).Value
Dim mypsr As PromptSelectionResult = mydwg.Editor.SelectWindow( _
myPPR, myPPR1)
If mypsr.Status = PromptStatus.OK Then
Using myTrans As Transaction = mydwg.TransactionManager.StartTransaction
For Each myObjectID As ObjectId In mypsr.Value.GetObjectIds
Dim myEnt As Entity = myObjectID.GetObject(OpenMode.ForWrite)
Next
End Using
End If
End Function
 
End Class

BillZndl

  • Guest
Re: Problem with group code
« Reply #1 on: November 29, 2011, 07:11:19 AM »
At a glance, if I understand you correctly, you want to create new groups?
You can add a new group to the database like this:

Code: [Select]


DBDictionary GrpDic = (DBDictionary)trans.GetObject(database.GroupDictionaryId, OpenMode.ForWrite);
Group NewGroup = new Group(NewName, true);
GrpDic.SetAt(NewName, NewGroup);
trans.AddNewlyCreatedDBObject(NewGroup, true);

//Then just add what entities that exist in the database to the group:

NewGroup.Append(entity.ObjectId);


Note: there are no error provisions with the above code.




Jeff H

  • Needs a day job
  • Posts: 6150
Re: Problem with group code
« Reply #2 on: November 29, 2011, 07:41:35 AM »
As Bill mentioned you were not appending any entites to the group, and you never append the group to the dictionary.
Your Selectobjectsforgroup function does not return anything so there is nothing to append.
I would change it to a ObjectIdcollection since it is a Selection Window.
 
Here is your code quickly modified.to get it working.
Check your PICKSTYLE setting.
 
Code: [Select]
        <CommandMethod("CG")> _
        Public Sub CreateGroup()
            Dim mydwg As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = mydwg.Database
            Dim myeditor As Editor = mydwg.Editor

            Using tr As Transaction = db.TransactionManager.StartTransaction()
                ' Get the group dictionary from the drawing
                Dim gd As DBDictionary = tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead)
                ' Check the group name, to see whether it's
                ' already in use
                Dim pso As New PromptStringOptions(vbLf & "Enter new group name: ")
                pso.AllowSpaces = True
                ' A variable for the group's name
                Dim grpName As String = ""
                Do
                    Dim pr As PromptResult = myeditor.GetString(pso)
                    ' Just return if the user cancelled
                    ' (will abort the transaction as we drop out of the using
                    ' statement's scope)
                    If pr.Status <> PromptStatus.OK Then
                        Return
                    End If
                    Try
                        ' Validate the provided symbol table name
                        SymbolUtilityServices.ValidateSymbolName(pr.StringResult, False)
                        ' Only set the block name if it isn't in use
                        If gd.Contains(pr.StringResult) Then
                            myeditor.WriteMessage(vbLf & "A group with this name already exists.")
                        Else
                            grpName = pr.StringResult
                        End If
                    Catch
                        ' An exception has been thrown, indicating the
                        ' name is invalid
                        myeditor.WriteMessage(vbLf & "Invalid group name.")

                    End Try
                Loop While grpName = ""
                ' Create our new group...

                Dim grp As New Group("Test group", True)
                ' Add the new group to the dictionary

                gd.UpgradeOpen()
                gd.SetAt(grpName, grp)
                Dim grpIds As ObjectIdCollection = Selectobjectsforgroup()
                grp.Append(grpIds)
                tr.AddNewlyCreatedDBObject(grp, True) ' Open the model-space
                Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
                Dim ms As BlockTableRecord = DirectCast(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)

                ' Commit the transaction
                tr.Commit()
            End Using
        End Sub
        Private Function Selectobjectsforgroup() As ObjectIdCollection
            Dim mydwg As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = mydwg.Database
            Dim myeditor As Editor = Application.DocumentManager.MdiActiveDocument.Editor
            Dim myPPR As Point3d = myeditor.GetPoint("Select 1st Point: ").Value
            Dim myPPR1 As Point3d = myeditor.GetCorner("Select 2nd Point: ", myPPR).Value
            Dim mypsr As PromptSelectionResult = mydwg.Editor.SelectWindow( _
            myPPR, myPPR1)
            Dim ids As New ObjectIdCollection()
            If mypsr.Status = PromptStatus.OK And mypsr.Value.GetObjectIds.Length > 0 Then
                Using myTrans As Transaction = mydwg.TransactionManager.StartTransaction
                    For Each myObjectID As ObjectId In mypsr.Value.GetObjectIds
                        ids.Add(myObjectID)
                    Next
                End Using
            End If
            Return ids
        End Function

 

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Problem with group code
« Reply #3 on: November 29, 2011, 07:42:45 AM »
Oh I forgot the most important thing
 
Welcome To the Swamp!!!

SOURCECODE

  • Guest
Re: Problem with group code
« Reply #4 on: November 29, 2011, 10:29:05 AM »
Thanks Bill for pointing me in the right direction. Thanks Jeff for showing me the error of my ways and getting the code to work.
Im very greatful.

vegbruiser

  • Guest
Re: Problem with group code
« Reply #5 on: February 15, 2012, 04:03:25 AM »
@Jeff, your group code is just what we needed.

However, once the group is created, is there any way of toggling the selectable option without using the AutoCAD Group command?

Thanks in advance.

Alex.


ACtually, it looks like modifying this:

Dim grp As New Group("Test group", True)

To this:

Dim grp As New Group("Test group", False)

should do the trick..?
« Last Edit: February 15, 2012, 04:06:48 AM by AlexF »

BillZndl

  • Guest
Re: Problem with group code
« Reply #6 on: February 15, 2012, 06:53:11 AM »
@Jeff, your group code is just what we needed.

However, once the group is created, is there any way of toggling the selectable option without using the AutoCAD Group command?

Thanks in advance.

Alex.


ACtually, it looks like modifying this:

Dim grp As New Group("Test group", True)

To this:

Dim grp As New Group("Test group", False)

should do the trick..?

I don't know if this will work for you but when I want to work on a group as individual entities,
I toggle the pickstyle variable (using a button on my pallete) from 3 to zero.

Code: [Select]
public static void PickStyleToggle()
        {           
            Int16 PickStyle = (Int16)AcadApp.GetSystemVariable("PickStyle");           

            if (PickStyle.Equals(0))
            {               
                AcadApp.SetSystemVariable("PickStyle", 3);               
                PartGroupsMonitor.PartMonitor.Instance.button10.BackColor = System.Drawing.Color.LightGreen;
                PartGroupsMonitor.PartMonitor.Instance.button10.Text = "Toggle PickStyle: 3";               
            }
            else
            {               
                AcadApp.SetSystemVariable("PickStyle", 0);
                PartGroupsMonitor.PartMonitor.Instance.button10.BackColor = System.Drawing.Color.Pink;
                PartGroupsMonitor.PartMonitor.Instance.button10.Text = "Toggle PickStyle: 0";
                Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
            }
        }

vegbruiser

  • Guest
Re: Problem with group code
« Reply #7 on: February 15, 2012, 07:27:26 AM »
Useful to know, Thanks Bill.

Incidentally, setting the "Selectable" option to False (as per my previous reply) does indeed control whether a group is selectable or not.

That'll be me needing to re-read the arxmgd.chm help file then. #RTFM  :oops:

BillZndl

  • Guest
Re: Problem with group code
« Reply #8 on: February 15, 2012, 09:33:12 AM »
Useful to know, Thanks Bill.

Incidentally, setting the "Selectable" option to False (as per my previous reply) does indeed control whether a group is selectable or not.

That'll be me needing to re-read the arxmgd.chm help file then. #RTFM  :oops:

Thanks, I never thought of doing that but sounds useful also.
That may be included in my next round of work on that project....if that day ever comes. :(