Prevent Block from edit from C# API

Prevent Block from edit from C# API

iPranavKulkarni
Advocate Advocate
996 Views
9 Replies
Message 1 of 10

Prevent Block from edit from C# API

iPranavKulkarni
Advocate
Advocate

How can we prevent block from edit?
Right now if I double click on block it can be edited. I tried to lock the layer and block but unable to do that.

also I have to prevent block from being copied.

Can anyone guide me in this?
I tried with:

1. _AcAp.DocumentLock Doclk = cDoc.LockDocument()

2. newLayer.IsLocked = true;

 

0 Likes
997 Views
9 Replies
Replies (9)
Message 2 of 10

norman.yuan
Mentor
Mentor

So, you want to prevent a block definition from being changed.

 

LockDocument() has nothing to do with preventing data in database being modified. It is meant to flag the code execution should occur in Application context or document context.

 

Nor setting layer being locked helps, because block definition (BlockTableRecord) is no an Entity, which must sit on a layer.

 

You would want to have a good reason as why you do not want to protect the block definition being changed WHILE your application is used in the AutoCAD session, because whatever protection you managed to use with your application, only have its effect while your application is loaded, and while your application is loaded, what business operation could lead to a CAD user to attempt to modify the said block definition(s).

 

If you still think it is absolutely necessary, you can look into ObjectOverrule and overwrite the its Open() and/or Close() method: if the object being opened/closed is the target BlockTableRecord for writing, you can raise an exception so that the Open()/Close() can be silently cancelled during runtime (in debugging, the code would break, though).

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 10

iPranavKulkarni
Advocate
Advocate
Thanks for quick reply.
I will try ObjectOverrule and see how can i prevent.
if I stuck somewhere will get back to you.

0 Likes
Message 4 of 10

iPranavKulkarni
Advocate
Advocate
I tried Object overrule for preventing block from erasing.
Although I am not sure It is right but it is working.
public override void Erase(DBObject dbObject, bool erasing)
{
//base.Erase(dbObject, erasing);
throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NotApplicable);
}
This will work for me.
but can you share some idea for copy or selection.
0 Likes
Message 6 of 10

iPranavKulkarni
Advocate
Advocate

@Gepaha Thanks for answer.

Is there any way to prevent to go to Block editor definition.

 

0 Likes
Message 7 of 10

iPranavKulkarni
Advocate
Advocate
Thanks I have tried copy overrule and it works good.
Is there any way that prevent to edit blocks (block editor)
0 Likes
Message 8 of 10

saurabhvithoba
Explorer
Explorer

Hi @iPranavKulkarni, did you get any solution for preventing Edit Blocks ? 

0 Likes
Message 9 of 10

_gile
Consultant
Consultant

@saurabhvithoba If the block definition (BlockTableRecord) is created by code, creating an anonymous block will prevent this block to be edited.

var blockDefinition = new BlockTableRecord(){ Name = "*U" };

This has some consequences: this block won't be inserable through the INSERT commands.

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 10

saurabhvithoba
Explorer
Explorer

Thanks @_gile for the quick reply. I will try this approach.

0 Likes