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

InvalidOperation exception

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
239 Views, 5 Replies

InvalidOperation exception

Im getting this exception (see attached bitmap) and Im not really sure what it mean
and so dont know where to look to fix it.
During a call to GetObject() I get "Invalid operation due to state of the object".
So what about the state of the object? its open for write and my "locals" window says
its a valid object id. So what else could be wrong?
Below is the function with a comment where the error occurs.
Thanks for any tips,
Perry
------------------------------------------------------------------------------------
///
/// Update/change a revision block reference
///

public void updateRev(string rev)
{
//We are being handed a string object which represents a revision record
//composed of 6 items delimited by a carat^. We will parse out those items
//into individual strings then use those strings to update the revision
//block that corresponds to the revleter which is the
//first item in the string passed in.
#if (DEBUG)
Utilities.prompt("\nIn revision update.");
#endif
bool found = false;
char[] delimiterChars = { '^', '\t' };
string[] words = rev.Split(delimiterChars);
string letter = words[0];
string date = words[1];
string note = words[2];
string by = words[3];
string check = words[4];
string zone = words[5];
//the original string may have come from a dictionary entry.
//if thats the case then the rev letter will be prefixed with a "REV-"
//so if its there, delete it.
if (letter.Contains("REV-"))
letter = letter.Substring(4, 1);
using (Database dBase = HostApplicationServices.WorkingDatabase)
{
using (Transaction trans = dBase.TransactionManager.StartTransaction())
{
//Now, drill into the database and obtain a reference to the BlockTable
BlockTable blkTbl = (BlockTable)trans.GetObject(dBase.BlockTableId, OpenMode.ForWrite);
if (blkTbl.Has("TBREVISION"))
{
BlockTableRecord blkTblRec = (BlockTableRecord)trans.GetObject(blkTbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite);
foreach (ObjectId objId in blkTblRec)
{
// For each object in the block table record, cast it to a block reference
DBObject dBaseObj = trans.GetObject(objId, OpenMode.ForWrite);//INVALIDOPERATION EXCEPTION on valid objId?
BlockReference blkRef = dBaseObj as BlockReference;

if (blkRef != null)
{
BlockTableRecord blkDef = (BlockTableRecord)trans.GetObject(blkRef.BlockTableRecord, OpenMode.ForWrite);
if (blkDef.Name.ToUpper() == "TBREVISION")
{
//Get the attribute references,if any
AttributeCollection attCol;
attCol = blkRef.AttributeCollection;
found = false;
if (attCol.Count > 0)
{
blkRef.UpgradeOpen();
foreach (ObjectId attId in attCol)
{
using (AttributeReference att = (AttributeReference)trans.GetObject(attId, OpenMode.ForWrite, false))
{
string txt = "";
txt = (att.TextString.ToString());
//If the attribute textstring matches the passed in rev letter
//make sure the rest of its strings are what we want
if (txt == letter)
{
found = true;
}
if (found)
{
if (att.Tag == "TB_REV_DATE")
{
if (att.TextString != date)
att.TextString = date;
}
if (att.Tag == "TB_REV_NOTE")
{
if (att.TextString != note)
att.TextString = note;
}
if (att.Tag == "TB_REV_BY")
{
if (att.TextString != by)
att.TextString = by;
}
if (att.Tag == "TB_REV_CHECK")
{
if (att.TextString != check)
att.TextString = check;
}
if (att.Tag == "TB_REV_ZONE")
{
if (att.TextString != zone)
att.TextString = zone;
}
}//if found
}
}//foreach attribute
trans.Commit();
}//if attributes
}//if blkdef == rev
blkDef.Dispose();
blkRef.Dispose();
}//if blkref not null
dBaseObj.Dispose();
}//foreach object
blkTblRec.Dispose();
}//if has tbrev
blkTbl.Dispose();
}//using trans
}//using dbase
}//end function
5 REPLIES 5
Message 2 of 6
Anonymous
in reply to: Anonymous

It seems to me that you wanted to modify an object in an event handler,
DocumentBecameCurrent. At that moment, we can't modify anything in the
database since the database and many objects are in a flux status. We can
read information of course. The only even handler that allows us to write or
change something is CommandEnded. Hope this explains.

Bill

"perry" wrote in message
news:5203482@discussion.autodesk.com...
Im getting this exception (see attached bitmap) and Im not really sure what
it mean
and so dont know where to look to fix it.
During a call to GetObject() I get "Invalid operation due to state of the
object".
So what about the state of the object? its open for write and my "locals"
window says
its a valid object id. So what else could be wrong?
Below is the function with a comment where the error occurs.
Thanks for any tips,
Perry
---------------------------------------------------------
---------------------------
///
/// Update/change a revision block reference
///

public void updateRev(string rev)
{
//We are being handed a string object which represents a
revision record
//composed of 6 items delimited by a carat^. We will parse out
those items
//into individual strings then use those strings to update the
revision
//block that corresponds to the revleter wh
ich is the
//first item in the string passed in.
#if (DEBUG)
Utilities.prompt("\nIn revision update.");
#endif
bool found = false;
char[] delimiterChars = { '^', '\t' };
string[] words = rev.Split(delimiterChars);
string letter = words[0];
string date = words[1];
string note = words[2];
string by = words[3];
string check = w
ords[4];
string zone = words[5];
//the original string may have come from a dictionary entry.
//if thats the case then the rev letter will be prefixed with a
"REV-"
//so if its there, delete it.
if (letter.Contains("REV-"))
letter = letter.Substring(4, 1);
using (Database dBase =
HostApplicationServices.WorkingDatabase)
{
using (Transaction trans = dBase.Trans
actionManager.StartTransaction())
{
//Now, drill into the database and obtain a reference
to the BlockTable
BlockTable blkTbl =
(BlockTable)trans.GetObject(dBase.BlockTableId, OpenMode.ForWrite);
if (blkTbl.Has("TBREVISION"))
{
BlockTableRecord blkTblRec =
(BlockTableRecord)trans.GetObject(blkTbl[BlockTableRecord.PaperSpace],
OpenMode.ForWrite);

foreach (ObjectId objId in blkTblRec)
{
// For each object in the block table record,
cast it to a block reference
DBObject dBaseObj = trans.GetObject(objId,
OpenMode.ForWrite);//INVALIDOPERATION EXCEPTION on valid objId?
BlockReference blkRef = dBaseObj as
BlockReference;

if (blkRef != null)
{

BlockTableRecord blkDef =
(BlockTableRecord)trans.GetObject(blkRef.BlockTableRecord,
OpenMode.ForWrite);
if (blkDef.Name.ToUpper() == "TBREVISION")
{
//Get the attribute references,if any
AttributeCollection attCol;
attCol = blkRef.AttributeCollection;

found = false;
if (attCol.Count > 0)
{
blkRef.UpgradeOpen();
foreach (ObjectId attId in attCol)
{
using (AttributeReference att =
(AttributeReference)trans.GetObject(attId, OpenMode.ForWrite, false))
{

string txt = "";
txt =
(att.TextString.ToString());
//If the attribute
textstring matches the passed in rev letter
//make sure the rest of its
strings are what we want
if (txt == letter)
{

found = true;
}
if (found)
{
if (att.Tag ==
"TB_REV_DATE")
{
if (att.TextString
!= date)

att.TextString = date;
}
if (att.Tag ==
"TB_REV_NOTE")
{
if (att.TextString
!= note)
att.TextString
= note;
}

if (att.Tag == "TB_REV_BY")
{
if (att.TextString
!= by)
att.TextString
= by;
}
if (att.Tag ==
"TB_REV_CHECK")
{

if (att.TextString != check)
att.TextString
= check;
}
if (att.Tag ==
"TB_REV_ZONE")
{
if (att.TextString
!= zone)

att.TextString = zone;
}
}//if found
}
}//foreach attribute
trans.Commit();
}//if attributes
}//if blkdef == rev
blkDef.Dispose();

blkRef.Dispose();
}//if blkref not null
dBaseObj.Dispose();
}//foreach object
blkTblRec.Dispose();
}//if has tbrev
blkTbl.Dispose();
}//using trans
}//using dbase
}//end function
Message 3 of 6
Mick Duprez
in reply to: Anonymous

You need to cast your 'getObject' to the correct type:
eg
DBObject pObj = (DBObject)trans.GetObject(.......

hth,
Mick. Message was edited by: Mick Duprez
Message 4 of 6
Anonymous
in reply to: Anonymous

Thanks for the tips guys, but I'm afraid neither helped.
Not sure why I didn't have the cast in there, but its there now.
No difference. As far as being called from within an event, thats
true, but even when I manually call the function when Acad is
"quiet", the same error occures. It should still work within the event
though as I have near duplicate code in an Arx version which is called
from within the "LoadDocument" event and it works fine. I have also
modified some other attributes from within an event handler.
Bummer, gonna have to keep digging 😞
Perry
Message 5 of 6
Anonymous
in reply to: Anonymous

If the code doesn't work either in a single command rather than in a event
handler, then there must be something else wrong. I noticed OpenForWrite
mode was always used no matter whether it's necessary in the code. I would
suggest changing them all to OpenForRead and updating the status to Write
when it's needed to see if it makes any difference. One other idea is to
just comment out those code regarding writing operation and see if the error
is still there. Once again, code works fine in a single command doesn't
necessarily meant it will work in an event handler or any other contexts. -
Bill

"perry" wrote in message
news:5206079@discussion.autodesk.com...
Thanks for the tips guys, but I'm afraid neither helped.
Not sure why I didn't have the cast in there, but its there now.
No difference. As far as being called from within an event, thats
true, but even when I manually call the function when Acad is
"quiet", the same error occures. It should still work within the event
though as I have near duplicate code in an Arx version which is called
from within the "LoadDocument" event and it works fine. I have also
modified some other attributes from within an event handler.
Bummer, gonna have to keep digging 😞
Perry
Message 6 of 6
Anonymous
in reply to: Anonymous

Bill Zhang wrote:
> If the code doesn't work either in a single command rather than in a event
> handler, then there must be something else wrong. I noticed OpenForWrite
> mode was always used no matter whether it's necessary in the code. I would
> suggest changing them all to OpenForRead and updating the status to Write
> when it's needed to see if it makes any difference. One other idea is to
> just comment out those code regarding writing operation and see if the error
> is still there. Once again, code works fine in a single command doesn't
> necessarily meant it will work in an event handler or any other contexts. -
> Bill
>
Yes, there is obviously (but not obvious enough) something else wrong.
I originally DID have the other two open modes as read, but changed them
to write to see if that helped. It didnt. However one thing I havent tried yet
is to make ALL the open modes read and do an upgrade open only when necessary.
I'll try that next. (first though I'll try your suggestion and make them all
read operations and comment out the "writing" stuff).
Perry

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