.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Xref and UCS

47 REPLIES 47
Reply
Message 1 of 48
Anonymous
811 Views, 47 Replies

Xref and UCS

Hi,

I'm new to .NET. I had been trying to create a dll to change the UCS and Layer when the user types XREF in the command line. I don't know how to go about it. I just tried the code below, but it is not working. Can anybody help me?

Imports System
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices

Imports acadApp = Autodesk.AutoCAD.ApplicationServices.Application

Public Class UCS
Public Sub XrefCommand()
If acadApp.DocumentManager.MdiActiveDocument.CommandInProgress = "XREF" Then
MsgBox("Done!")
End If
End Sub
End Class

Thanks Message was edited by: cadprog
47 REPLIES 47
Message 21 of 48
Anonymous
in reply to: Anonymous

I'm sorry, I didn't read your post clearly. It's my mistake. Thanks for being so patient with me.

You had said about iterating the documents. I tried that too, but...no luck...may be I couldn't do it with my not very intelligent brain. This is what I did.

Imports System
Imports Microsoft.VisualBasic
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports AcadApp = Autodesk.AutoCAD.ApplicationServices.Application

Namespace UCS
Public Class UCS

Implements IExtensionApplication

Public Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize
Try
Dim docEnum As IEnumerator = Application.DocumentManager.GetEnumerator
While docEnum.MoveNext
Dim Doc As Document = CType(docEnum.Current, Document)
AddHandler AcadApp.DocumentManager.DocumentCreated, AddressOf DocumentManager_DocumentCreated
AddHandler AcadApp.DocumentManager.DocumentToBeDestroyed, AddressOf DocumentManager_DocumentToBeDestroyed
End While
Catch ex As Exception

End Try

End Sub

Sub DocumentManager_DocumentToBeDestroyed(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)

RemoveHandler e.Document.CommandWillStart, AddressOf Document_CommandWillStart

End Sub

Sub DocumentManager_DocumentCreated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)

AddHandler e.Document.CommandWillStart, AddressOf Document_CommandWillStart

End Sub

Sub Document_CommandWillStart(ByVal sender As Object, ByVal e As CommandEventArgs)
AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("" & Chr(10) & "**** CommandWillStart: {0}****" & Chr(10) & "", e.GlobalCommandName)

End Sub

Public Sub Terminate() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate

End Sub

End Class

End Namespace

Thanks
Message 22 of 48
Anonymous
in reply to: Anonymous

Your code is attempting to add handlers for
DocumentCreated and DocumentToBeDestroyed
to each document. Why?

The event you're handling for each document is
the CommandWillStart event.

Also, where did you learn to use IEnumerator
to iterate over a collection?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5542260@discussion.autodesk.com...
I'm sorry, I didn't read your post clearly. It's my mistake. Thanks for being so patient with me.

You had said about iterating the documents. I tried that too, but...no luck...may be I couldn't do it with my not very intelligent brain. This is what I did.

Imports System
Imports Microsoft.VisualBasic
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports AcadApp = Autodesk.AutoCAD.ApplicationServices.Application

Namespace UCS
Public Class UCS

Implements IExtensionApplication

Public Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize
Try
Dim docEnum As IEnumerator = Application.DocumentManager.GetEnumerator
While docEnum.MoveNext
Dim Doc As Document = CType(docEnum.Current, Document)
AddHandler AcadApp.DocumentManager.DocumentCreated, AddressOf DocumentManager_DocumentCreated
AddHandler AcadApp.DocumentManager.DocumentToBeDestroyed, AddressOf DocumentManager_DocumentToBeDestroyed
End While
Catch ex As Exception

End Try

End Sub

Sub DocumentManager_DocumentToBeDestroyed(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)

RemoveHandler e.Document.CommandWillStart, AddressOf Document_CommandWillStart

End Sub

Sub DocumentManager_DocumentCreated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)

AddHandler e.Document.CommandWillStart, AddressOf Document_CommandWillStart

End Sub

Sub Document_CommandWillStart(ByVal sender As Object, ByVal e As CommandEventArgs)
AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("" & Chr(10) & "**** CommandWillStart: {0}****" & Chr(10) & "", e.GlobalCommandName)

End Sub

Public Sub Terminate() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate

End Sub

End Class

End Namespace

Thanks
Message 23 of 48
Anonymous
in reply to: Anonymous

I didn't find the DocumentAdded Event. So thought it should be iterated with IEnumerator.

Is this correct?

Public Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize

Dim Docs As DocumentCollection = Application.DocumentManager
Dim Doc As Document = Application.DocumentManager.MdiActiveDocument

For Each Doc In Docs
AddHandler AcadApp.DocumentManager.DocumentCreated, AddressOf DocumentManager_DocumentCreated
AddHandler AcadApp.DocumentManager.DocumentToBeDestroyed, AddressOf DocumentManager_DocumentToBeDestroyed
Next

End Sub

Message was edited by: cadprog Message was edited by: cadprog
Message 24 of 48
Anonymous
in reply to: Anonymous

No problem if it doesn't work on the active doc when the dll was loaded. I would like someone to guide me on how to change the UCS to World and the Active Layer to 0 when the command XREF is entered, and set it back to original once xref is finished. It's urgent! Please help!

Thanks
Message 25 of 48
jspark
in reply to: Anonymous

I am curious as to why you would add an event for each open document when only the active document would ever receive an XREF command... It's overkill, overhead, and more difficult to code...

...
Dim WithEvents mThisDrawing as Document = ....
...
Private Sub DocumentCollection_DocumentBecameCurrent...
mThisDrawing = AcadApp.DocumentManager.MdiActiveDocument
End Sub

Done.

The hill is not so steep after all...
Message 26 of 48
Anonymous
in reply to: Anonymous

If VB.NET automatically wires up the events for you
each time the value of mThisDrawing changes, then
that might make sense.

But since I don't use VB, I don't know if that's the
case. In C#, that doesn't happen and I would have to
not only assign the variable, but also add the event
handlers to the document if they've not been added
already.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5740001@discussion.autodesk.com...
I am curious as to why you would add an event for each open document when only the active document would ever receive an XREF command... It's overkill, overhead, and more difficult to code...

...
Dim WithEvents mThisDrawing as Document = ....
...
Private Sub DocumentCollection_DocumentBecameCurrent...
mThisDrawing = AcadApp.DocumentManager.MdiActiveDocument
End Sub

Done.

The hill is not so steep after all...
Message 27 of 48
jspark
in reply to: Anonymous

Since there is no "WithEvents" in C#, you will need to add/remove events manually on DocumentBecameCurrent. You should still only be dealing with the active document/previous active document.
Message 28 of 48
Anonymous
in reply to: Anonymous

>> You should still only be dealing with the
>> active document/previous active document.

Sorry, that's not the case In C#.

Rather than jump through all of those hoops every
time the active document changes, we only have to
add event handlers to each document once, when
the document is created.

I also don't see the point to using 'WithEvents' to
add/remove events from each document as they are
activated/deactivated.

Whether VB wires up the code to do that for you, or
not, really isn't the issue. The issue is that its a lot
of work that only needs to be done once, not every
time a document is activated.

Also, you have the wrong event.

The 'current' document and the 'active' document
are two different things.

A document can be made the current document
without ever being activated.

DocumentActivated along with a test to see if the
argument is already the active document, is the
event that should be used, if you insist on going
that way.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
Message 29 of 48
jspark
in reply to: Anonymous

>>"I also don't see the point to using 'WithEvents' to
>>add/remove events from each document as they are
>>activated/deactivated."

If you declare thisObj WithEvents, when the value of thisObj
changes, the associated events now apply to the new value of thisObj. There is no adding or removing events period. You simply change the value of thisObj. Can you see the point now? If in C# you can declare a handler to handle one of thisObj's events, I beleive you will get the same effect if you change the value of thisObj...

After your long explanation of the VBA "ThisDrawing", I would think you would see the power of it, and try to replicate some of its functionality in .NET... It's not hard to do.

>>"Rather than jump through all of those hoops every
>>time the active document changes, we only have to
>>add event handlers to each document once, when
>>the document is created."

There are no hoops if you understand my above response. Why would you want 20 events listening for an XREF command when you only need one. That's just bad programming. I don't care if it's VB or C#. You only need ONE event to listen for an XREF command. You are beating around the bush because you don't want to admit that your approach is suboptimal. After reading many of your posts on this forum, I am quite happy to point this out to you and everyone else.
Message 30 of 48
Anonymous
in reply to: Anonymous

>> If you declare this WithEvents, when the value of
>> this changes, so does the associated events. There
>> is no adding or removing events period.

Perhaps you didn't read or understand what I said earlier
about multiple instances of your class, one for each open
document.

That's why I asked if you knew how an instance of
your class was being created. If you do not know how
it is, then go back and read my other response again.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
Message 31 of 48
jspark
in reply to: Anonymous

My application object is a single instance, and it hadles only the active document. That's what it was designed to do. You are changing the subject. Just admit that your approach is suboptimal. You can do it.
Message 32 of 48
Anonymous
in reply to: Anonymous

Your application object is not a single instance.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5743518@discussion.autodesk.com...
My application object is a single instance, and it hadles only the active document. That's what it was designed to do. You are changing the subject. Just admit that your approach is suboptimal. You can do it.
Message 33 of 48
jspark
in reply to: Anonymous

My application object is a single instance. I have implemented IExtensionApplication, and something very visible happens on-screen each time an instance of my application object is created. Seriously though admit it.
Message 34 of 48
Anonymous
in reply to: Anonymous

Sorry, you're wrong.

The code you posted has a non-shared command
method called 'Start', and that means that there
will be an instance of the object containing that
method created for each open document in which
the command is issued.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5743542@discussion.autodesk.com...
My application object is a single instance. I have implemented IExtensionApplication, and something very visible happens on-screen each time an instance of my application object is created. Seriously though admit it.
Message 35 of 48
jspark
in reply to: Anonymous

That example was hand typed, not copied from my project. I'd be surprised if it compiled without errors. I assure you sir, my app object is a single instance, now admit it. If you created a variable in C# that always held a reference to the active document, you could wire up one event to that variable and be done with it.
Message 36 of 48
Anonymous
in reply to: Anonymous

>> That example was hand typed, not copied from my project.

Lol.. Yeah sure.

As far as the WithEvents nonsense goes, as I said, whether
you have to write the code to add/remove the events, or
whether VB writes the code to do that for you, the code is
still there (just look at it in Reflector if you don't believe it),
and must still (needlessly) execute each time a document
is activated.

The point wasn't that the code had to be written, the point
is that the code must exist and execute in the first place
(and it does, because VB generates it for you and because
I can see it in the disassembly). Hence, the code that you
actually deploy must still needlessly jump through hoops
every time a document is switched, rather than only once,
when a document is created.

We don't worry about how much code we have to write in
C#, we worry about how much garbage code is generated
by the compiler that executes needlessly, only to make
things slightly less complicated for the um... 'programmer'.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5743549@discussion.autodesk.com...
I'd be suppressed if it compiled without errors. I assure you sir, my app object is a single instance, now admit it. If you created a variable in C# that always held a reference to the active document, you could wire up one event to that variable and be done with it.
Message 37 of 48
jspark
in reply to: Anonymous

You are dodging the issue aaaagain. The issue is not only that your approach takes more code, it is also that your approach is over kill and overhead. With your code, each inactive document is listening for an XREF command that will never EVER come.

FORGET WithEvents, it's confusing you. This is how to do it in C#, as I said above:

If you created a variable in C# that always held a reference to the active document, you could wire up one event to that variable and be done with it.

Must I explain how to keep that variable up to date with the active document or do you get it yet?
Message 38 of 48
cadMeUp
in reply to: Anonymous

This is written in C#, you can adapt it over to VB:

Editor editor = AcadApp.DocumentManager.MdiActiveDocument.Editor;
Database db = AcadApp.DocumentManager.MdiActiveDocument.Database;
TransMan transMgr = db.TransactionManager;

// For changing the current layer.
using (Transaction trans = transMgr.StartTransaction())
{
string layerName = "NEW LAYER";

LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
if (!lt.Has(layerName))
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = layerName;

lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
}

AcadApp.SetSystemVariable("CLAYER", layerName);

trans.Commit();
}

// Change the current UCS (this will change the ucs to be aligned with the front view):
Point3d pnt = new Point3d(0, 0, 0);
Vector3d vx = new Vector3d(1, 0, 0);
Vector3d vy = new Vector3d(0, 0, 1);
Vector3d vz = new Vector3d(0, -1, 0);

Matrix3d ucsMat = Matrix3d.AlignCoordinateSystem(
Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
pnt, vx, vy, vz);

editor.CurrentUserCoordinateSystem = ucsMat;
Message 39 of 48
Anonymous
in reply to: Anonymous

>> You are dodging the issue aaaagain. The issue is not only
>> that your approach takes more code, it is also that your
>> approach is over kill and overhead.

Sorry, I'm not dodging the issue, you're just in denial
about the fact that your ideas about programming is
just more of the typical VB mindset, where it doesn't
matter how much garbage code is generated and must
execute, to make things easy for you, or what it costs
the end-user in the long run.

Also, you don't seem to understand what 'overhead' is.

Overhead is code that executes needlessly only so that
you don't have to write a few more lines of it.

>> With your code, each inactive document is listening for
>> an XREF command that will never EVER come.

Sorry, but you're mistaken again.

Documents that are not active cannot listen for events,
because they are not active. When a document is not
active, none of the things that trigger its events can
occur to start with, because the document is NOT ACTIVE.

Documents that are not active, are in a state of suspended
execution (or perhaps you might understand it better if we
say 'suspended animation'). In other words, the code that
continuously runs and listens for events, is not running
when a document is not active.

Perhaps you should consider familiarizing yourself with
a single-threaded cooperative fiber mode application, and
how only one of the lightweight threads associated with
each of its documents can execute at any given time.

To delve further into your misunderstanding of how events
work in general, a document is always listening for events
(like an "XREF command"), whether there are handlers for
those events, or not.

Duh.

>> FORGET WithEvents, it's confusing you.

No, I think its more a case everything is confusing you,
starting with the multiple instances of your class that
you tried to slither out of having to admit you didn't
even realize was happening.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
Message 40 of 48
jspark
in reply to: Anonymous

I notice that each time you reply, you refuse acknowledge this:

"If you created a variable in C# that always held a reference to the active document, you could wire up one event to that variable and be done with it.

Must I explain how to keep that variable up to date with the active document or do you get it yet?"

It must be because it is a far better solution to the OP's problem then that garbage you posted above.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost