Author Topic: Insert block that exists in drawing  (Read 3061 times)

0 Members and 1 Guest are viewing this topic.

cannorth

  • Guest
Insert block that exists in drawing
« on: February 13, 2014, 06:22:09 PM »
Hello,

  How do I insert a block that has its template already existing in the drawing?

Code: [Select]
        Try
            db = Application.DocumentManager.MdiActiveDocument.Database
            '        Using tr As Transaction = db.TransactionManager.StartTransaction()
            bt = CType(tr.GetObject(db.BlockTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead), BlockTable)
            bFound = bt.Has(sBlockName)

            If bFound Then
                Dim xDB As New Database(False, True)
                blockId = db.Insert(ParseField(sBlockName, 1, "."), xDB, True)
                Return True
            End If

  At the db.Insert line, .Net raises an exception.  It says eNullObjectId.

Thanks,

cannorth
« Last Edit: February 14, 2014, 11:34:55 AM by cannorth »

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Insert block that exists in drawing
« Reply #1 on: February 13, 2014, 07:00:02 PM »
Are you wanting to overwrite a existing BlockTableRecord with a block drawing?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Insert block that exists in drawing
« Reply #2 on: February 13, 2014, 07:04:30 PM »
cannorth,
You have commented the StartTransaction line .. and still use the tr variable later.
Is the Transaction assignment made elsewhere ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

cannorth

  • Guest
Re: Insert block that exists in drawing
« Reply #3 on: February 13, 2014, 08:11:52 PM »
The transaction assignment is made earlier.  I'm not trying to overwrite.

cannorth

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

cannorth

  • Guest
Re: Insert block that exists in drawing
« Reply #5 on: February 13, 2014, 08:54:25 PM »
With this link:

http://www.theswamp.org/index.php?topic=37686.msg427184#msg427184

What namespace does the .Scale use?

cannorth

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Insert block that exists in drawing
« Reply #6 on: February 13, 2014, 09:06:35 PM »
I can't see a .Scale there.

I see a scale variable of type double
and I see a  br.ScaleFactors = new Scale3d(scale);

First things first : did you get your problem resolved?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

cannorth

  • Guest
Re: Insert block that exists in drawing
« Reply #7 on: February 14, 2014, 11:34:42 AM »
Thanks, I'm using a scale variable now.

When I write this code down:

Code: [Select]
Autodesk.AutoCAD.Geometry.Matrix3d.PreMultiplyBy

.Net complains "Reference to a non-shared member requires an object reference."

cannorth

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Insert block that exists in drawing
« Reply #8 on: February 14, 2014, 11:54:02 AM »
PreMultiplyBy is a member of MatrixXD and need to use on a object.

Dim mat as Matrix3D = new Matrix3D
mat.PreMultiplyBy.......

cannorth

  • Guest
Re: Insert block that exists in drawing
« Reply #9 on: February 14, 2014, 11:59:34 AM »
Thanks, I did that and now I have this bug:

 mat.PreMultiplyBy(ed.CurrentUserCoordinateSystem)

"Too many arguments to " the above function.

cannorth

n.yuan

  • Bull Frog
  • Posts: 348
Re: Insert block that exists in drawing
« Reply #10 on: February 14, 2014, 03:24:28 PM »
The OP's question and the cote he shows are quite confusing:

Does he want to insert a block definition (thus Database.Insert()), or a BlockReference (as a reference to a BlockTableRecord)?

According to the description, it seems the Block definition already exists in the drawing, and he want to CREATE a BlockReference. To ordinary CAD user, the term is "insert a block", but to us programmer, "Insert block" is ambiguous, to say the least.

The code itself is contradicting itself:

           If bFound Then
                Dim xDB As New Database(False, True)
                blockId = db.Insert(ParseField(sBlockName, 1, "."), xDB, True)
                Return True
            End If

If bFound is "true", meaning the block definition already there, why insert a database as a block definition?

If the purpose to overwrite existing block definition with a different drawing file (named the same as the block definition), then there is not need to test if the current drawing database has the block definition already existing or not. If so, the above code is still wrong: after newing an empty database, Database.ReadDwgFile() must be called before inserting it into another drawing as block definition.

cannorth

  • Guest
Re: Insert block that exists in drawing
« Reply #11 on: February 14, 2014, 08:07:26 PM »

I want to insert a BlockReference (as a reference to a BlockTableRecord).

The code for Matrix3D has helped me.

cannorth