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

In C#, what class is work with the entity of "INSERT"?

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
449 Views, 5 Replies

In C#, what class is work with the entity of "INSERT"?

Hi,
I want to insert a block into the database, what I know is the name of block(dwg's name), how can I finish it by use C#?
I cannot find the relative class of "INSERT", as the class 'Line' with "LINE" relatively.
Thanks in advance.
5 REPLIES 5
Message 2 of 6
Anonymous
in reply to: Anonymous

You could search this newsgroup for articles about:
- creating a block definition using the class 'BlockTableRecord'
- inserting an instance of this definition in the drawing using the class
'BlockReference'



schreef in bericht news:5575256@discussion.autodesk.com...
Hi,
I want to insert a block into the database, what I know is the name of
block(dwg's name), how can I finish it by use C#?
I cannot find the relative class of "INSERT", as the class 'Line' with
"LINE" relatively.
Thanks in advance.
Message 3 of 6
caddie75
in reply to: Anonymous

Hi,

here are some of the classes:
BlockTable > database table of all blocks in the drawing.
BlockTableRecord > a Single block in the database.

BlockReference > the block placed in the drawing, a "reference" to the blocktablerecord.

A Caddie.
Message 4 of 6
Anonymous
in reply to: Anonymous

Here you go: (I insert the other file exploded, but you can omit this step)

internal DBObjectCollection InsertDWGFile(string dwgName, Point2d insertion, double rotation, Scale3d blockScale, string layer)
{
try
{
// Find the file
string fullDWGName = HostApplicationServices.Current.FindFile(Properties.Settings.Default.BlockFolder + "\\" + dwgName + ".dwg", dataBase, FindFileHint.Default);
// Create block's file database
Database blockDB = new Database(false, false);
blockDB.ReadDwgFile(fullDWGName, System.IO.FileShare.Read, true, "");
// Insert block definition
ObjectId blockID = dataBase.Insert(dwgName, blockDB, true);
// Close block file
blockDB.Dispose();
// Insert a block instance
BlockReference blockInstance = new BlockReference(new Point3d(insertion.X, insertion.Y, 0), blockID);
blockInstance.Rotation = rotation;
blockInstance.ScaleFactors = blockScale;
blockInstance.Layer = layer;

// Explode the block instance
DBObjectCollection entities = new DBObjectCollection();
blockInstance.Explode(entities);
// Delete the block definition
blockInstance.Dispose();
// Change the layer of the block's entities
foreach (Entity entity in entities)
if (entity.Layer == "0") entity.Layer = layer;
// Delete the block reference from the database
currentTransaction.GetObject(blockID, OpenMode.ForWrite).Erase(true);
return entities;
}
// If no file was found
catch (Autodesk.AutoCAD.Runtime.Exception)
{
MText errorText = new MText();
errorText.Location = new Point3d(insertion.X, insertion.Y, 0);
errorText.Rotation = rotation;
errorText.TextHeight = TextHeight;
errorText.Contents ="Failed to locate file:\\P" +
(Properties.Settings.Default.BlockFolder + "\\" +
dwgName).Replace("\\", "\\\\") + ".dwg"; errorText.Attachment = AttachmentPoint.MiddleCenter;
errorText.Layer = layer;
DBObjectCollection rtrn = new DBObjectCollection();
rtrn.Add(errorText);
return rtrn;
}
}
Message 5 of 6
Anonymous
in reply to: Anonymous

Thank you very much.
I Try it and get the correct result, but I get another trouble, that is: when I insert a block into current database, I want to retrieve the name of block, code list as:
protected void GetBlkName()
{
using (Transaction trans = transMan.StartTransaction())    //start transaction
{
BlockReference blk = (BlockReference)trans.GetObject(ObjectId, OpenMode.ForRead, false);
BlockTableRecord blkDefine = (BlockTableRecord)trans.GetObject(blk.BlockId, OpenMode.ForRead);
blockName = blkDefine.Name;
}
}
But unfortunately, I get an incorrect result, can you tell me how to do?
Thanks.
Message 6 of 6
Anonymous
in reply to: Anonymous

Your code is almost correct.
If you change "blk.BlockId" to "blk.BlockTableRecord" it will work.



schreef in bericht news:5576312@discussion.autodesk.com...
Thank you very much.
I Try it and get the correct result, but I get another trouble, that is:
when I insert a block into current database, I want to retrieve the name of
block, code list as:
protected void GetBlkName()
{
using (Transaction trans =
transMan.StartTransaction())????//start transaction
{
BlockReference blk =
(BlockReference)trans.GetObject(ObjectId, OpenMode.ForRead, false);
BlockTableRecord blkDefine =
(BlockTableRecord)trans.GetObject(blk.BlockId, OpenMode.ForRead);
blockName = blkDefine.Name;
}
}
But unfortunately, I get an incorrect result, can you tell me how to
do?
Thanks.

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