Author Topic: BricsCAD returning BlockReference  (Read 2522 times)

0 Members and 1 Guest are viewing this topic.

Bert

  • Guest
BricsCAD returning BlockReference
« on: February 10, 2015, 11:00:32 AM »
Hello guys,

I've got a good amount of .NET code written for AutoCAD 2009 wherein methods return BlockReference objects.
These BlockReferences stayed valid & initialized after being returned. In BricsCAD however, the reference to the BlockReference is lost after the transaction is closed.
A bit of code explains it better :
Code - Visual Basic: [Select]
  1.     Sub dummyExample()
  2.         'Lets grab a BlockReference based on a given ObjectId
  3.        Dim myBlockRef As BlockReference = findBlockRef(someObjectId)
  4.         'AutoCAD allows me to consult/call the properties & methods of the obtained BlockReference after this point
  5.        Console.WriteLine(myBlockRef.Name) 'AutoCAD = Ok with this
  6.  
  7.         'Bricscad 'lost' reference to the BlockReference when the transaction in findBlockRef() was closed.
  8.  
  9.     End Sub
  10.  
  11.     'Dummy Function
  12.    Public Function findBlockRef(_acadBlockRefId As ObjectId) As BlockReference
  13.  
  14.         Dim returnBlockRef As BlockReference
  15.         ' Get the current document and database, and start a transaction
  16.        Dim acDoc As Document = Bricscad.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  17.         Dim acCurDb As Database = acDoc.Database
  18.         ' Lock the document
  19.        Using acLckDoc As DocumentLock = acDoc.LockDocument()
  20.             ' Start a transaction in the new database
  21.            Using acTrans = acDoc.TransactionManager.StartTransaction()
  22.                 returnBlockRef = acTrans.GetObject(_acadBlockRefId, OpenMode.ForRead)
  23.                 ' Commit the transaction
  24.                acTrans.Commit()
  25.             End Using ' Dispose of the transaction
  26.        End Using ' Unlock the document
  27.  
  28.         Return returnBlockRef
  29.     End Function
  30.  

So this might not be 'best-practice' in the first place ?
Why does AutoCAD & BricsCAD function differently here ?
What can I do to 'translate' my code best ?

Thanks for reading, any & all suggestions are welcome !

Regards,
Bert

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: BricsCAD returning BlockReference
« Reply #1 on: February 10, 2015, 11:55:03 AM »
Hi,

I don't know how BricsCAD .NET API deals with memory, perhaps it forces the Garbage Collector to collect as soon as the Dispose of the Transaction while AutoCAD let it run independently.

Anyway, as you probably know every object opened with a Transaction or add to a Transaction will be disposed when the transaction is disposed, so I think it's a very bad practice to make a function return a DBObject if this function isn't called from within the Transaction used to open this DBObject.
In this sense, BricsCAD seems to have a better behavior if it does not allow this kind of practice.
Speaking English as a French Frog

Bert

  • Guest
Re: BricsCAD returning BlockReference
« Reply #2 on: February 12, 2015, 10:17:32 AM »
merci Gile,

I've got lots of functions that get\insert\draw\create BlockReferences in run-time.
Normally these BlockReferences get returned or added in collections along the way, to use later in code.

What would you suggest ? Should I work (return\collect) the ObjectId's of these BlockReferences instead ?

In any case, i've got a lot of refactoring to do !

Regards,
Bert

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: BricsCAD returning BlockReference
« Reply #3 on: February 12, 2015, 04:34:57 PM »
I was going to suggest ObjectId's before but wasn't sure of the context of your method.
If you are going to 'store' objects definitely use ObjectId's, if you are passing the object to another method within the transaction then the object is still within scope of the transaction so it should be ok.
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

Bert

  • Guest
Re: BricsCAD returning BlockReference
« Reply #4 on: February 13, 2015, 02:29:52 AM »
Ok, thanks for the advice guys !

Bert

huiz

  • Swamp Rat
  • Posts: 913
  • Certified Prof C3D
Re: BricsCAD returning BlockReference
« Reply #5 on: February 13, 2015, 03:32:04 AM »
BricsCAD is indeed reacting different. Your ReturnBlockRef is empty or not available after the Commit.

ObjectId is the best option to return.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.