1. Yes, that is a bug that if you execute "HLColor" before "MyHLOn". IN the "HLColor" command method, I am supposed to start with something like below as the first line of code:
If (_hlOverrule==null) return;
2. Locking document. SInce the custom HighlightOverrule might be used in other programs, such as a modeless window, where locking document might be required, so I simply do the locking inside the Highlight/Unhighlight, so that the other programs that use this component would not have to lock document just because of the custom highlight/unhighlight. Of course if the other programs need to lock document because of their own process, there would be nested locking, and there may be penalties on Acad performance, but it would be safer and easier for the other programs to use this component.
Another possible approach you can look into: HighlightOverrule.
I just posted some simple code on this here:
http://drive-cad-with-code.blogspot.com/2010/09/highlight-autocad-entity-in-your-own.html
Hi Norman,
wonder why it's necessary to lock the document, since the operation is on the current doc.
Hope you dont't mind that I've ported your example to f#, which allows a succinct expression of the singleton pattern required in Overrule and avoids the pitfalls associated with properties on uninitialized objects. What happens if you execute HLColor before MyHLOn? <evil grin>
// Originally c# by Norman Yuan
// http://drive-cad-with-code.blogspot.com/2010/09/highlight-autocad-entity-in-your-own.html
open Autodesk.AutoCAD.DatabaseServices
open Autodesk.AutoCAD.EditorInput
open Autodesk.AutoCAD.Runtime
type acApp = Autodesk.AutoCAD.ApplicationServices.Application
type MyHighlightOverrule() as this =
inherit HighlightOverrule()
let mutable _colorIndex = 1
let mutable _oldColorIndex = 1
do MyHighlightOverrule.AddOverrule(
RXClass.GetClass typeof<Polyline>, this, true )
member x.ColorIndex
with set v = _colorIndex <- v
and get() = _colorIndex
override x.Highlight(entity, subId, highlightAll) =
match entity with
| :? Polyline as pline ->
let doc = acApp.DocumentManager.MdiActiveDocument
use dl = doc.LockDocument()
use tr = doc.TransactionManager.StartTransaction()
pline.UpgradeOpen()
_oldColorIndex <- pline.ColorIndex
pline.ColorIndex <- _colorIndex
pline.DowngradeOpen()
base.Highlight(entity, subId, highlightAll)
| _ -> ()
override x.Unhighlight(entity, subId, highlightAll) =
match entity with
| :? Polyline as pline ->
let doc = acApp.DocumentManager.MdiActiveDocument
use dl = doc.LockDocument()
use tr = doc.TransactionManager.StartTransaction()
pline.UpgradeOpen()
pline.ColorIndex <- _oldColorIndex
pline.DowngradeOpen()
base.Unhighlight(entity, subId, highlightAll)
| _ -> ()
module Commands =
let hlOverRule = lazy(new MyHighlightOverrule())
[<CommandMethod "MyHLOn">]
let TurnOnMyHighlight() =
hlOverRule.Force() |> ignore
MyHighlightOverrule.Overruling <- true
[<CommandMethod "MyHLOff">]
let TurnOffMyHighlight() =
hlOverRule.Force() |> ignore
MyHighlightOverrule.Overruling <- false
[<CommandMethod "HLColor">]
let SetHighlightColor() =
let ed = acApp.DocumentManager.MdiActiveDocument.Editor
let pir =
new PromptIntegerOptions(
"\nEnter color index (a number form 1 to 7):",
LowerLimit = 1,
UpperLimit = 7 )
|> ed.GetInteger
if pir.Status = PromptStatus.OK then
hlOverRule.Value.ColorIndex <- pir.Value
Cheers, Thorsten