What is the main reason for wanting to use transactions?
I would be considered a "newbie" when it comes to AutoLisp so I have no understanding of what problem transactions would solve in AutoLisp.
Transactions just wrap calls to open, close, cancel.
A transaction is a abstraction of a set of operations and you can achieve the same result performing the operations yourself saving cycles.
Transaction do not make your code faster they will make it slower. More code has to execute and more memory has to be allocated for a transaction to be used. Transactions make writing code faster and easier to reuse and the time loss and extra complexity added is usually not enough compared to the gains of using them.
You could argue that it will only open a object once for all operations making it quicker or whatever but that is in the context of writing a reusable method or whatever but it still is a longer path for executing a set of operations.
You can achieve the same result from creating a collection and adding every object you open and checking to see if it is already open then returning object if it is or open it add to collection then return it. The built in transaction creates a new managed wrapper for each open so you could probably save time there by returning the same managed wrapper.
DocumentLocking is the mechanism used for setting Undo marks.
The is why the code I posted in earlier post will have unexpected results if UNDO is called. It will roll back to state before previous command was called.
I can make it so there are multiple transactions and subordinate transactions operating at the same time, and I can control them just like in .net.
I do not know of anyway of doing that because you only can use the Top Transaction or most recent transaction created per document.
The only way I could get it to behave correctly was to use UNDO command(Begin, End) or use DocumentManager.ExecuteInApplicationContext where a document lock would not be nested inside previous command.
ResultBuffer returnbuffer
= new ResultBuffer
(new TypedValue
((int)LispDataType
.Nil));
[LispFunction("SetProps")]
public ResultBuffer SetProps(ResultBuffer buffer = null)
{
Application.DocumentManager.ExecuteInApplicationContext(ApplicationContextSetProps, buffer);
return returnbuffer;
}
private void ApplicationContextSetProps(object rbuffer)
{
ResultBuffer buffer = rbuffer as ResultBuffer;
if (buffer == null) return;
using (DocumentLock docLok = Doc.LockDocument(DocumentLockMode.ProtectedAutoWrite, "LISPSETPROPS", "LISPSETPROPS", true))
{
TypedValue[] args = buffer.AsArray();
int numberOfArguments = args.Length;
if (numberOfArguments > 2 & numberOfArguments % 2 != 0)
{
ObjectId id = (ObjectId)args[0].Value;
try
{
using (Transaction trx = Doc.TransactionManager.StartTransaction())
{
DBObject dbo = trx.GetObject(id, OpenMode.ForWrite);
Type type = dbo.GetType();
PropertyInfo propInfo;
string propName;
for (int i = 1; i < numberOfArguments; i = i + 2)
{
propName = args[i].Value as string;
propInfo = type.GetProperty(propName);
propInfo.SetValue(dbo, args[i + 1].Value, null);
}
trx.Commit();
}
}
catch (System.Exception)
{
}
}
returnbuffer
= new ResultBuffer
(new TypedValue
((int)LispDataType
.Int16,
0));
}
}