My favourite pastime: reverse error engineering. Didn't work here, that is, my code works as intended, so where did I go wrong?
Except by using the wrong language and skipping the jig part...
[<CommandMethod("DIATEST", CommandFlags.Session)>]
let diatest() =
let fstDoc = acadApp.DocumentManager.MdiActiveDocument
use openFileDia =
new OpenFileDialog(
Filter = "Drawing (*.dwg)|*.dwg|All files (*.*)|*.*",
Multiselect = false,
RestoreDirectory = true )
if openFileDia.ShowDialog() = DialogResult.OK &&
not(System.String.IsNullOrEmpty openFileDia.FileName) then
let docs = acadApp.DocumentManager
let sndDoc = docs.Open(openFileDia.FileName, true)
docs.MdiActiveDocument <- sndDoc
let sBlock = System.IO.Path.GetFileNameWithoutExtension openFileDia.FileName
let msg = "Would you like to insert the drawing " + sBlock + " as a block?"
let res =
MessageBox.Show(
msg, "Find block", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1 )
if res = DialogResult.Yes then
docs.MdiActiveDocument <- fstDoc
use doclock = fstDoc.LockDocument()
let oid =
fstDoc.Database.Insert(
SymbolUtilityServices.BlockModelSpaceName,
sBlock,
sndDoc.Database,
true )
if not oid.IsNull then
let pt0 = ref Point3d.Origin
use tr = fstDoc.Database.TransactionManager.StartTransaction()
let cspace =
tr.GetObject(fstDoc.Database.CurrentSpaceId, OpenMode.ForWrite)
:?> BlockTableRecord
let insert = new BlockReference(Point3d.Origin, oid)
cspace.AppendEntity insert |> ignore
tr.AddNewlyCreatedDBObject(insert, true)
let sset = SelectionSet.FromObjectIds[| insert.ObjectId |]
let ppr =
fstDoc.Editor.Drag(
sset, "Drag me",
fun pt (mat: Matrix3d byref) ->
if !pt0 = pt then SamplerStatus.NoChange
else
mat <- Matrix3d.Displacement(pt - !pt0)
SamplerStatus.OK )
if ppr.Status = PromptStatus.OK then
insert.Position <- ppr.Value
else
insert.Erase()
tr.Commit()