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

c# block insert

17 REPLIES 17
Reply
Message 1 of 18
Anonymous
4677 Views, 17 Replies

c# block insert

Can anybody help me

Can anybody help me, the following source code doesn't work and I understand
not what is the error.



[CommandMethod("AsdkCmd1")]

static public void test()

{

string comp = "d:\temp\test.dwg";

Document doc = acadApp.DocumentManager.MdiActiveDocument;

Editor ed = doc.Editor;

Transaction tr = doc.TransactionManager.StartTransaction();

try

{

string dwgName =
HostApplicationServices.Current.FindFile(comp,acadApp.DocumentManager.MdiActiveDocument.Database,FindFileHint.Default);

Database db = new Database(false, false);

db.ReadDwgFile(dwgName,System.IO.FileShare.Read, true, "");

ObjectId NewBlkId = new ObjectId();

NewBlkId = doc.Database.Insert(dwgName, db, false);

BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId,
OpenMode.ForRead,true);

BlockTableRecord btr
=(BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);

BlockReference bref = new BlockReference(new
Autodesk.AutoCAD.Geometry.Point3d(10,10,0), NewBlkId);

btr.AppendEntity(bref);

tr.AddNewlyCreatedDBObject(bref, true);

tr.Commit();

}

finally

{

tr.Dispose();

}

}

Best regards and thanks

Ernst
17 REPLIES 17
Message 2 of 18
Anonymous
in reply to: Anonymous

Maybe this sample help you: http://through-the-interface.typepad.com/through_the_interface/2006/08/import_blocks_f.html
Message 3 of 18
egug
in reply to: Anonymous

hi

thanks, but this code doesn't work
Message 4 of 18
Anonymous
in reply to: Anonymous

If this code doesn't work for you - send your's explanation of this problem to Kean Walsmley.
Message 5 of 18
Anonymous
in reply to: Anonymous

What about change tthis line:
string comp = "d:\temp\test.dwg";
with:
string comp = "d:\\temp\\test.dwg";
Message 6 of 18
Anonymous
in reply to: Anonymous

the paramaters for "WblockCloneObjects" have changed a bit.
Just a guess since you didn't feel it was relevant to say were the
error is.
wrote in message news:5307554@discussion.autodesk.com...
hi

thanks, but this code doesn't work
Message 7 of 18
egug
in reply to: Anonymous

and now the running program 🙂

using System;
using System.Runtime;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Diagnostics;
using System.Globalization;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;


[assembly: CommandClass(typeof(ClassLibrary.Class))]

namespace ClassLibrary
{
///
/// Summary description for Class.
///

public class Class
{
public Class()
{
//
// TODO: Add constructor logic here
//
}

// Define Command "AsdkCmd1"
[CommandMethod("AsdkCmd1")]
static public void test()
{
string comp = "D:\\Temp\\test.dwg";
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Transaction tr = doc.TransactionManager.StartTransaction();
try
{
string dwgName = HostApplicationServices.Current.FindFile(comp,acadApp.DocumentManager.MdiActiveDocument.Database,FindFileHint.Default);
Database db = new Database(false, false);
db.ReadDwgFile(dwgName,System.IO.FileShare.Read, true, "");
ObjectId BlkId;
BlkId = doc.Database.Insert(dwgName, db, false);
BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead,true);
BlockTableRecord btr =(BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
BlockReference bref = new BlockReference(new Autodesk.AutoCAD.Geometry.Point3d(10,10,0), BlkId);
btr.AppendEntity(bref);
tr.AddNewlyCreatedDBObject(bref, true);
bref.ExplodeToOwnerSpace();
bref.Erase();
tr.Commit();
}
finally
{
tr.Dispose();
}
}
}
}
Message 8 of 18
Anonymous
in reply to: Anonymous

Thanks for the code.
I rewrote it to VB, but it doesn't work. Where did I make a the mistake?


Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime

Imports acadApp = Autodesk.AutoCAD.ApplicationServices.Application

Public Class GSClass

' Define command 'Asdkcmd1'
_
Public Sub Asdkcmd1()
Dim db As Database
Dim BlkId As ObjectId

Dim comp As String = "c:\cadproductie\c20\applicaties\cadensys\symb\prorail\50a.dwg"
Dim doc As Document = acadApp.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Try
Dim dwgName As String = HostApplicationServices.Current.FindFile(comp, acadApp.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default)
' Read file in new database
db = New Database(False, False)
db.ReadDwgFile(dwgName, System.IO.FileShare.Read, True, "")
' insert block in drawing database
BlkId = doc.Database.Insert(dwgName, db, False)
Dim bt As BlockTable = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead, True)
Dim btr As BlockTableRecord = tr.GetObject(BlkId, OpenMode.ForWrite)
Dim bref As BlockReference = New BlockReference(New Autodesk.AutoCAD.Geometry.Point3d(10, 10, 0), BlkId)
btr.AppendEntity(bref)
tr.AddNewlyCreatedDBObject(bref, True)
bref.ExplodeToOwnerSpace()
bref.Erase()
tr.Commit()
Finally
tr.Dispose()
End Try
End Sub

End Class
Message 9 of 18
NathTay
in reply to: Anonymous

What do you mean by does not work? Are you getting an error? If so what is the error?

Regards - Nathan
Message 10 of 18
Anonymous
in reply to: Anonymous

Sorry, The code compiles and you can run/step through it. No error is reported, but no block is inserted in the drawing.
Message 11 of 18
Anonymous
in reply to: Anonymous

Try this

Imports AcDb = Autodesk.AutoCAD.DatabaseServices
Imports AcApp = Autodesk.AutoCAD.ApplicationServices.Application
Imports AcGe = Autodesk.AutoCAD.Geometry

Shared Function InsertExplodeDrawing(ByVal dwgName As String, _
ByVal insPt As AcGe.Point3d, _
ByVal s3d As AcGe.Scale3d, _
ByVal rot As Double) As AcDb.ObjectId

Dim id As AcDb.ObjectId = Nothing
Using db As AcDb.Database =
AcDb.HostApplicationServices.WorkingDatabase()
Using tr As AcDb.Transaction =
db.TransactionManager.StartTransaction
Try
Using sourceDb As AcDb.Database = New
AcDb.Database(False,
False)
sourceDb.ReadDwgFile(dwgName, FileShare.Read, True,
"")
id = db.Insert(dwgName, sourceDb, False)
End Using
Dim bt As AcDb.BlockTable =
tr.GetObject(db.BlockTableId,
AcDb.OpenMode.ForRead, True)
Dim btr As AcDb.BlockTableRecord =
tr.GetObject(db.CurrentSpaceId, AcDb.OpenMode.ForWrite, True)
Dim bref As AcDb.BlockReference = New
AcDb.BlockReference(insPt, id)
bref.ScaleFactors = s3d
bref.Rotation = rot
btr.AppendEntity(bref)
tr.AddNewlyCreatedDBObject(bref, True)
bref.ExplodeToOwnerSpace()
bref.Erase()
tr.Commit()
Catch ex As Exception
AcApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbLf
& ex.ToString)
End Try
End Using
End Using

End Function

tp



I'm protected by SpamBrave
http://www.spambrave.com/
Message 12 of 18
Anonymous
in reply to: Anonymous

Thanx tp.

It compiles and the block is inserted.

Gerard van der Sel
Message 13 of 18
gilseorin
in reply to: Anonymous

I have one question.
NO.1 works well.
No.2 gets error message.
Is there any solution for inserting with the different scale factor?
Thanks in advance.

1. Dim s3d As AcGe.Scale3d = New AcGe.Scale3d(10, 10, 10)
2. Dim s3d As AcGe.Scale3d = New AcGe.Scale3d(10, 20, 30)
Message 14 of 18
jimanderson
in reply to: egug

Hello All,

I am new to .NET for AutoCAD but have been doing AutoLISP for 20+ years and just started C#.NET.  I am using VS2010 (Building 3.5) and AutoCAD 2012. I am trying to insert a title block in a new drawing. I am getting a "eNoInputFiler" error on line 'db.ReadDwgFile(dwgName, System.IO.FileShare.Read, true, null);'

Can you me help?

        // Set the DWG to insert
                string comp = "C:\\DATA\\TB11X17A.dwg";
                Document doc = acadApp.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                Transaction tr = doc.TransactionManager.StartTransaction();
                try
                {
                    string dwgName = HostApplicationServices.Current.FindFile(comp, acadApp.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default);
                    Database db = new Database(false, false);
                    db.ReadDwgFile(dwgName, System.IO.FileShare.Read, true, null);
                    ObjectId BlkId;
                    BlkId = doc.Database.Insert(dwgName, db, false);
                    BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead, true);
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    BlockReference bref = new BlockReference(new Autodesk.AutoCAD.Geometry.Point3d(10, 10, 0), BlkId);
                    btr.AppendEntity(bref);
                    tr.AddNewlyCreatedDBObject(bref, true);
                    bref.ExplodeToOwnerSpace();
                    bref.Erase();
                    tr.Commit();
                }
                finally
                {
                    tr.Dispose();
                }

Message 15 of 18
Irvin
in reply to: jimanderson

Hi Jim,

 

Look at this post:

 

http://forums.autodesk.com/t5/NET/Discussion-Group-ClassLibrary-please-read/td-p/3005908

 

It has an class for inserting blocks.

 

Hope it helps,

 

Kind regards,

 

Irvin

Message 16 of 18
jimanderson
in reply to: Irvin

Thank you...  I will dig into it tonight and see if I can get it to work...  Thank you again!!!

Message 17 of 18
jimanderson
in reply to: jimanderson

It works!  Thanks everyone

Message 18 of 18
farrukhw
in reply to: egug

Thanks dude..Smiley Happy

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