I have a portion of my routine below. Basically I need the user to continue to loop through the command. They are picking Circles or blocks in the drawing and I get the insertion point, then insert a new block. However, whenever they try and use a transparent zoom or pan to move around the drawing when the promptentityresult is hit, it zooms back to the original location. How do I stop it from doing that? Any help or direction is much appreciated. Here is a code....
<
CommandMethod("getpnt", CommandFlags.Session)> _
Sub findpnt()
Dim cnt AsInteger = 0
Dim NEWPNT AsPoint3d = GetInsPoint()
While cnt < 3
NEWPNT = GetInsPoint()
' other code is inserted here...
cnt = cnt + 1
EndWhile
EndSub
Public Function GetInsPoint()
Dim inspnt AsPoint3d
Dim acdoced AsDocument = Application.DocumentManager.MdiActiveDocument
Dim myed AsEditor = acdoced.Editor
Dim db AsDatabase = HostApplicationServices.WorkingDatabase
Using mytrans2 AsTransaction = db.TransactionManager.StartTransaction()
Dim myPEO AsNewPromptEntityOptions(vbLf & "Select Circle or Block: ")
myPEO.SetRejectMessage(
"You must select a Circle or Block.")
myPEO.AddAllowedClass(
GetType(Circle), True)
myPEO.AddAllowedClass(
GetType(BlockReference), True)
myPEO.AllowNone =
False
Dim symbolent AsPromptEntityResult = myed.GetEntity(myPEO)
If symbolent.Status = PromptStatus.OK Then
Dim Objid AsObjectId = symbolent.ObjectId
Dim ent AsEntity = CType(mytrans2.GetObject(Objid, OpenMode.ForRead), Entity)
Dim enttype1 AsString = ent.GetType().ToString
Dim enttype AsString = Objid.ObjectClass.DxfName
SelectCase enttype
Case"CIRCLE"
Dim circleobj AsCircle = mytrans2.GetObject(Objid, OpenMode.ForRead)
inspnt = circleobj.Center
Case"INSERT"
Dim blockent AsBlockReference = mytrans2.GetObject(Objid, OpenMode.ForRead)
inspnt = blockent.Position
EndSelect
EndIf
EndUsing
Return inspnt
EndFunction
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
This is an interesting issue I have not noticed before and did not know current view seems associated with a new started transaction.
The reason AutoCAD always restore back to original view after each pick is that you did not commit the Transaction in the GetInsPoint() function.
As rule of thumb, one should always EXPLICITLY complete the transaction, either commit it, or abort it. If a transaction is wrapped in Using ... End Using, when End Using is reached, if the transaction has not been commited, it will be aborted before being disposed, which may not be what you want, as your code behaves.
So, you need to add mytrans2.Commit() right before End Using.
Norman Yuan
I forgot to mention: I actually tried your code.
Not "maybe". It IS because of the transaction not being commited.
Norman Yuan
That was it.....found a couple other locations that I did not commit my transactions. Still learning .NET as you can tell.
Once I got those cleared up, the routine is working like it should and is not zooming automatically!
Thanks so much for your help.
Excellent, Norman!
I thought that this is due to CommandFlags.Session, but you're absolutely right.
Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"
Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
I dug up this discussion because I find it's extremely annoying. It does not work like built-in commands. For example, if I start a LINE command and zoom on a specific part of my drawing, I stay where I am if I cancel the command. If I do the same in a .NET command with a transaction started, the transaction is aborted (to cancel changes done on the drawing) and I go back to the location I was before starting the command. Of course, I can handle it with nested transactions, but I found this behavior very strange...
Workaround:
// Top transaction (records view changes) using (Transaction tr = db.TransactionManager.StartTransaction()) { try {
// Retrieve entities, read-only operations
using (Transaction writeTr = db.TransactionManager.StartTransaction()) {
// Change database here. If something goes wrong or user cancels // the command, throw an exception and abort the writeTr transaction.
writeTr.Commit(); } } finally { // Always commit to preserve view changes tr.Commit(); } }