.NET

Reply
Valued Contributor
scarta
Posts: 52
Registered: ‎11-25-2008
Message 1 of 13 (367 Views)

Alternative Attribute command

367 Views, 12 Replies
05-29-2012 06:20 AM

Hi, I want write a command for attribute editing. The command check if the block name is equal to a string and if yes make some operation, if no continue with DDEDIT standard command.

The program work well if the block name is not the some of the string. When the name is the some the program make the correct operation but then run also the ddedit command.

there is a way for break the command?

 

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.Geometry

Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.AutoCAD.Colors

Imports System

Imports System.Windows.Forms

Imports System.Math

'Imports System.Web.Mail

Imports Microsoft.Office.Core

Imports Microsoft.Office.Interop

'Imports Microsoft.Office.Interop.Outlook

Imports System.Runtime.InteropServices

Imports Autodesk.AutoCAD.Interop

Public ClassreactorDim bEditCommand AsBooleanDim bDoRepositioning AsBoolean

<CommandMethod("AddEvents")> _

 

PublicSubplantDbEvents()

 

Dim doc AsDocument = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

 

Dim db AsDatabase = HostApplicationServices.WorkingDatabase()

 

'AddHandler db.ObjectOpenedForModify, New ObjectEventHandler(AddressOf objOpenedForMod)AddHandler doc.CommandWillStart, NewCommandEventHandler(AddressOfcmdWillStart)

 

'AddHandler doc.CommandEnded, New CommandEventHandler(AddressOf cmdEnded)

bEditCommand =

False

bDoRepositioning =

FalseEndSubPublicSub cmdWillStart(ByVal o AsObject, ByVal e AsCommandEventArgs)

 

Dim ed AsEditor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor

 

TryIf e.GlobalCommandName = "DDEDIT"Or e.GlobalCommandName = "EATTEDIT"Then

 

Dim fr2 AsNewFerriDim strNomeBlocco AsStringDim myDwg AsDocument'Dim myBT As BlockTable'Dim myBTR As BlockTableRecordDim myDB As DatabaseServices.DatabaseDim myTransMan As DatabaseServices.TransactionManagerDim myTrans As DatabaseServices.TransactionDim myEd As EditorInput.EditorDim myPSR As EditorInput.PromptSelectionResult

 

Dim TargetBlockRef As DatabaseServices.BlockReference

myDB = ApplicationServices.

Application.DocumentManager.MdiActiveDocument.Database

myDwg = Autodesk.AutoCAD.ApplicationServices.

Application.DocumentManager.MdiActiveDocument

strNomeBlocco =

"FERRI-P"

myTransMan = myDB.TransactionManager

myTrans = myTransMan.StartTransaction

myEd = myDwg.Editor

a2: myPSR = myEd.SelectImplied

SelectCasemyPSR.Status

 

Case EditorInput.PromptStatus.OK

 

SelectCasemyPSR.Value.Count

 

Case1

 

Dim mySelobj As EditorInput.SelectedObjectDim myAcadEnt As DatabaseServices.Entity

 

For I = 1 TomyPSR.Value.Count

mySelobj = myPSR.Value.Item(I - 1)

myAcadEnt = mySelobj.ObjectId.GetObject(DatabaseServices.

OpenMode.ForRead)

TargetBlockRef = myAcadEnt

If Microsoft.VisualBasic.Left$(UCase(TargetBlockRef.Name), 5) = "FERRI"Then

 

 

fr2.Show()

GoToA15

 

EndIf

 

 

NextI

 

CaseIs> 1

MsgBox(

"Seleziona un solo blocco delle Posizioni")

myPSR =

NothingGoToa2

 

Case ElseEndSelectCase EditorInput.PromptStatus.Error

 

' GoTo a3

 

' Case EditorInput.PromptStatus.Cancel

 

EndSelectEndIfCatch ex As System.Exception

ed.WriteMessage(

"Error in cmdWillStart: "+ ex.Message)

 

EndTry

A15:

EndSub

End

Class

*Expert Elite*
norman.yuan
Posts: 1,014
Registered: ‎04-27-2009
Message 2 of 13 (357 Views)

Re: Alternative Attribute command

05-29-2012 07:58 AM in reply to: scarta

Handling CommandWillStart event will not meet your need, as you have already found out, because no matter what you do in the event handler, it is just something you can do before the command starts, after your code runs, the command will still run.

 

To your need, you can handle DocumentCollection.DocumentLockModeChanged event and DocumentCollection.DocumentLockModeChangeVetoed event. That is, in the DocumentLockModeChanged event handler, if a certain condition is met, you can then veto the command, which will result in the DocumentLockModeChangeVetoed event being fired. Then in DocumentLockModeChangeVetoed event handler, you can start your own process/custom command.

 

Some psuedo code:

 

private static bool runAlternativeCmd=false;

 

public void Initialize()

{

    DocumentCollection docs=Application.DocumentManager;

    docs.DocumentLockModeChanged+=new DocumentLockModeChangedEventHandler(DocLockChanged);

    docs.DocumentLockModeChangeVetoed+=new DocumentLockModeChangeVetoedEventHandler(DocLockChangeVetoed);

}

 

void DocLockChanged(object sender, DocumentLockModeChangedEventArgs e)

{

    if (e.GlobalCommandName.ToUpper()=="XXXXXXX")

    {

       //Test if the entity in the implied selectionset is the targeting block. If it is, veto the command

       runAlternativeCmd=true;

    }

    else

   {

      runAlternativeCmd=false;

   }

}

 

void DocLockChaneVetoed(object sender, DocumentLockChangeVetoedEventArgs e)

{

    If (runAlternativeCmd)

    {

        Document doc=Application.DocumentManager/MdiDocument;

        doc.SendStringToExcute("MyAttrEdit ",true, false, false);

    }

}  

 

[CoommandMethod["MyAttrEdit")]

public static void EditMyAttribute()

{

    Do my own editing process

}

 

HTH

*Expert Elite*
GTVic
Posts: 917
Registered: ‎09-07-2004
Message 3 of 13 (344 Views)

Re: Alternative Attribute command

05-29-2012 10:43 AM in reply to: norman.yuan

Here are two ADN DevBlog entries:

 

Get the real name of a block if it is anonymous (necessary for dynamic blocks):

http://adndevblog.typepad.com/autocad/2012/05/identifying-block-name-from-the-block-reference.html

 

Veto a command as described above:

http://adndevblog.typepad.com/autocad/2012/05/veto-a-particular-command-in-autocad.html

Valued Contributor
scarta
Posts: 52
Registered: ‎11-25-2008
Message 4 of 13 (323 Views)

Re: Alternative Attribute command

05-30-2012 05:55 AM in reply to: GTVic
 
Valued Contributor
scarta
Posts: 52
Registered: ‎11-25-2008
Message 5 of 13 (321 Views)

Re: Alternative Attribute command

05-30-2012 06:01 AM in reply to: GTVic

Thamks

I must use the DLL with AutoCAD 2013 but there is an error

I have convert C code in Vb.net code, this is the result:

 

 

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.Geometry

Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.AutoCAD.Colors

Imports System

Imports System.Windows.Forms

Imports System.Math

'Imports System.Web.Mail

Imports System.Runtime.InteropServices

Imports Autodesk.AutoCAD.Interop

Imports System.Collections.Specialized

Public ClassClass2

<CommandMethod("AddVetoToQuit")> _

 

PublicSharedSubAddVetoToQuit()

 

Dim doc AsDocumentCollection = ApplicationServices.Application.DocumentManager

doc.DocumentLockModeChanged +=NewDocumentLockModeChangedEventHandlerdoc_DocumentLockModeChanged)

 

 EndSub

 

 

 

<CommandMethod("RemoveVetoToQuit")> _

 

PublicSharedSubRemoveVetoToQuit()

 

Dim doc AsDocumentCollection = ApplicationServices.Application.DocumentManager

 

 

doc.DocumentLockModeChanged -=NewDocumentLockModeChangedEventHandler(doc_DocumentLockModeChanged)

 

 

EndSub

 

 

 

PrivateSharedSub doc_DocumentLockModeChanged(sender AsObject, e AsDocumentLockModeChangedEventArgs)

 

IfString.Compare(e.GlobalCommandName, "QUIT", True) = 0 Then

 

 

e.Veto()

EndIf

EndSub

 

 

End Class

*Expert Elite*
norman.yuan
Posts: 1,014
Registered: ‎04-27-2009
Message 6 of 13 (314 Views)

Re: Alternative Attribute command

05-30-2012 06:36 AM in reply to: scarta

I do not think you can use

 

Object.Event +=EventHandler(...) in VB.NET.

 

You should look into AddHandler/RemoveHandler statement in VB.NET, like:

 

AddHandler Object.Event, Address Of EventHandlerName

RemoveHandler Object.Event, Address Of EventHandlerName

Valued Contributor
scarta
Posts: 52
Registered: ‎11-25-2008
Message 7 of 13 (310 Views)

Re: Alternative Attribute command

05-30-2012 07:33 AM in reply to: norman.yuan

l

Valued Contributor
scarta
Posts: 52
Registered: ‎11-25-2008
Message 8 of 13 (310 Views)

Re: Alternative Attribute command

05-30-2012 07:35 AM in reply to: scarta

Like this? But I receive an error on "Autodesk.AutoCAD.ApplicationServices.DocumentCollection.DocumentLockModeChanged"

Public

Classveto

<

CommandMethod("AddDocColEvent")> _

 

PublicSubAddDocColEvent()

 

AddHandler Autodesk.AutoCAD.ApplicationServices.DocumentCollection.DocumentLockModeChanged, AddressOfdocColDocAct

 

EndSub

<

CommandMethod("RemoveDocColEvent")> _

 

PublicSubRemoveDocColEvent()

 

RemoveHandler ApplicationServices.Application.DocumentManager.DocumentActivated, _

 

AddressOfdocColDocAct

 

EndSubPublicSub docColDocAct(ByVal senderObj AsObject, _

 

ByVal docColDocActEvtArgs AsDocumentCollectionEventArgs)

ApplicationServices.

Application.ShowAlertDialog(docColDocActEvtArgs.Document.Name & _

 

" was activated.")

 

EndSub

End

Class

*Expert Elite*
norman.yuan
Posts: 1,014
Registered: ‎04-27-2009
Message 9 of 13 (305 Views)

Re: Alternative Attribute command

05-30-2012 07:52 AM in reply to: scarta

Well, carefully looking into the compiling error would reveal something obviously.

 

1. You added event handler to handle DocumentLockModeChanged event, but you remove a handler on DocumentActivated event, which is never been added in the first place.

 

2. The event handler docColDocAct() cannot be used to handle DocumentLockModeChanged event, because the handler's signature is wrong. Event handler for DocumentLockModeChanged requires the second argumnet of the handler to be type of DocumentLockModeChangedEventArgs, not DocumentCollectionEventArgs.

Valued Contributor
scarta
Posts: 52
Registered: ‎11-25-2008
Message 10 of 13 (293 Views)

Re: Alternative Attribute command

05-31-2012 12:47 AM in reply to: norman.yuan

Have you a VB.NET example?

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.