Valued Contributor
Posts: 55
Registered: ‎05-14-2012
Message 1 of 4 (397 Views)
Accepted Solution

Call AutoCAD refedit command by clicking ONCE on a block reference

397 Views, 3 Replies
02-18-2014 05:55 PM

Hi All,


I am in the process of adding functionality to my AutoCAD plugins developed in CAD 2010 using VS 2010. Everything looks good, till I attempted to infuse AutoCAD's refedit command into one of my routines.


My routine is simple enough: user clicks my toolbutton, and is prompted to pick a block. User picks the block, and I update a specific attribute in the block. However, I am now required to pop up AutoCAD's refedit command to enable the user edit the block as well.


I have written some code to accomplish this, and all seems well, except for a small hiccup I encountered. I discovered while testing, that when I execute my command, I find that I have to click on a block twice before the AutoCAD refedit command pops up its dialogue. The first click executes my command, while a second click on the same block then displays the refedit dialogue.


My question then: Is there any way for me to adjust my code such that it requires only ONE CLICK to both perform my command AND at the same time pop up AutoCAD's block editor? Instead of click-click?


I post my code below for clarity:


Public Sub editReference()

    Dim db as Database = HostApplicationServices.WorkingDatabase
    Dim acTrans as Transaction = db.TransactionManager.StartTransaction()

    Dim acDoc As Document = acApp.DocumentManager.MdiActiveDocument

    Dim ed As Editor = acDoc.Editor


    Using acTrans
        ' Pick the block ref object using the pickObject() method. pickObject() is illustrated immediately after this sub.
        If Not pickObject() Then Exit Sub ' blockRef below is a global variable of type BlockReference, set in the pickObject() method


        If Not blockRef Is Nothing Then
            If blockRef.AttributeCollection.Count > 0 Then
                cat.editAttribute("LENGTH", "120", blockRef) ' This method successfully edits the blockRef's LENGTH attribute, no problem...

                ed.WriteMessage(" Section length successfully updated." & vbCrLf)


                ' Call the AutoCAD block edit command
                acDoc.SendStringToExecute("_REFEDIT ", False, False, False)
            End If
        End If

    End Using

End Sub


Private Function pickObject() As Boolean
    pickObject = True

    Dim res As PromptEntityResult
    Dim opts As PromptEntityOptions = New PromptEntityOptions("")
    opts.AllowNone = True

    Dim acDoc As Document = acApp.DocumentManager.MdiActiveDocument

    opts.Message = vbLf & "Pick a block: "
    res = acDoc.Editor.GetEntity(opts)


    ' Exit if user presses ESC or cancels command
    If res.Status = PromptStatus.Cancel Then
        pickObject = False
        Exit Function
    End If


    ' Set the picked entity
    Dim id As ObjectId = res.ObjectId
    If id.ObjectClass.Name = "AcDbBlockReference" Then
    blockRef = TryCast(acTrans.GetObject(id, OpenMode.ForRead), BlockReference) ' blockRef is a global variable

End Function



As you can see from my code in the editReference() sub, I call acDoc's SendStringToExecute on _REFEDIT, which correctly pops up the refedit dialogue, but then it pops up only after the user clicks TWICE on the same block reference.


Any ideas on how to accomplish this in one click instead of two? I would gladly furnish any additional information.



Many thanks in advance,



Valued Contributor
Posts: 55
Registered: ‎05-14-2012
Message 2 of 4 (392 Views)

Re: Call AutoCAD refedit command by clicking ONCE on a block reference

02-18-2014 06:10 PM in reply to: mindofcat

NB: Still looking for a way to solve this problem, I commented out all the code within my USING block in the editReference() sub previously posted, leaving only the refedit command thus:


Using acTrans

    acDoc.SendStringToExecute("_REFEDIT ", False, False, False)
End Using


And when I ran the command in AutoCAD, the refedit dialogue pops up in one click. This is the desired effect, except that, now I am unable to run my update attribute command since I don't have a handle on the block reference selected from the AutoCAD refedit command.

However, I think I will be able to accomplish this IF I can find a way to return an object id from the refedit command.


Once I have an object id returned to my transaction from the acDoc.SendStringToExecute("_REFEDIT ", False, False, False) command, I am confident I will be able to perform my cat.editAttribute() command on the block ref selected from refedit.


I am now doing some research to see if there is a way to return or get object id of block reference selected during AutoCAD's refedit command (or even the block ref itself), but so far, I have been unable to come up with the results I am looking for. Hope this sheds some more light on my problem...


Any ideas?


Many thanks in advance.

Valued Contributor
Posts: 55
Registered: ‎05-14-2012
Message 3 of 4 (358 Views)

Re: Call AutoCAD refedit command by clicking ONCE on a block reference

02-20-2014 08:30 AM in reply to: mindofcat

Further research has whittled down the problem to this:


Instead of using acDoc.SendStringToExecute("_REFEDIT ", False, False, False) to call the refedit command, I find that I can use the acDoc.AcadDocument.SendCommand(strCommand) instead to accomplish this.


My problem now is now to build the strCommand string such that I can call refedit and at the same time pass it the handle or object id of my picked block. If this can be accomplished, then I do not have to worry about the prompt to 'pick reference' from the AutoCAD refedit command.


I tried something to this effect already:

Dim strHandle As String = blockRef.ObjectId.Handle.ToString()
strHandle = "(handent """ & strHandle & """)"
Dim strCommand As String = "_refedit" + vbCr + strHandle + vbCr

With the modification above, I test the sub in AutoCAD. It performs my editAttribute functionality alright, but then the AutoCAD command line tells me 'Reference not found' and then it still prompts me to 'Select reference' which means I still have to click twice instead of once...

I am close, I can feel it. SendCommand is the key to solving this dilemma, but I have never used it before, and have no idea how to get it to call refedit for me whilst passing in my picked block handle (or object id) to it.

Any ideas?

Many thanks for your help...

Valued Contributor
Posts: 55
Registered: ‎05-14-2012
Message 4 of 4 (350 Views)

Re: Call AutoCAD refedit command by clicking ONCE on a block reference

02-20-2014 11:18 AM in reply to: mindofcat

Found my solution at TheSwamp. This code below:


Dim cmdString As String = "_refedit" & Environment.NewLine & _
pickedPoint.ToString.Trim("(", ")") & Environment.NewLine


Solves my problem.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community