.NET

Reply
Active Contributor
tangferry
Posts: 43
Registered: ‎04-21-2005
Message 1 of 3 (243 Views)

How to insert a blockreference with attibutes?

243 Views, 2 Replies
11-27-2005 09:05 PM
I want to insert a blockreference with attibutes.
The following are the codes:
public class Class1
{
[CommandMethod("INSRTBLK")]
public void InsertBlk()
{
Database curDb = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptResult stringRes = ed.GetString("\nEnter Block Name: ");
string blkName = stringRes.StringResult;
if (stringRes.Status != PromptStatus.OK && blkName.Length == 0)
{
ed.WriteMessage("\nInvalid block name.");
return;
}
DBTrans tm = curDb.TransactionManager;
using (Transaction trans = tm.StartTransaction())
{
BlockTable blkTable = (BlockTable)tm.GetObject(curDb.BlockTableId, OpenMode.ForRead);
if (blkTable.Has(blkName) == false)
{
ed.WriteMessage(string.Format("\nBlock definition {0} not found. ", blkName));
return;
}
ObjectId blkId = blkTable[blkName];
BlockTableRecord blkTableRecord = (BlockTableRecord)tm.GetObject(blkTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
PromptPointOptions pointOpt = new PromptPointOptions("\nPick insertion point: ");
pointOpt.AllowNone = false;
PromptPointResult pointRes = ed.GetPoint(pointOpt);
BlockReference insrtObj = new BlockReference(pointRes.Value, blkId);
BlockTableRecord blkDefRecord = (BlockTableRecord)tm.GetObject(blkId, OpenMode.ForRead);
if (blkDefRecord.HasAttributeDefinitions)
{
foreach (ObjectId defId in blkDefRecord)
{
AttributeDefinition attDef = tm.GetObject(defId, OpenMode.ForRead) as AttributeDefinition;
if (attDef != null && attDef.Constant == false)
{
AttributeReference att = new AttributeReference();
att.SetPropertiesFrom(attDef);
att.Invisible = attDef.Invisible;
Point3d basePt = attDef.Position;
basePt = basePt + insrtObj.Position.GetAsVector();
att.Position = basePt;
att.Height = attDef.Height;
att.Rotation = attDef.Rotation;
string tagPrompt = attDef.Tag;
att.Tag = tagPrompt;
tagPrompt = attDef.Prompt;
ed.WriteMessage(string.Format("{0}{1}", "\n", tagPrompt));
att.FieldLength = 25;
att.TextString = "This is a test";
insrtObj.AttributeCollection.AppendAttribute(att);
}
}
}
blkTableRecord.AppendEntity(insrtObj);
tm.AddNewlyCreatedDBObject(insrtObj, true);
trans.Commit();
}
}
}


But when i run it in the AutoCAD,I get the errors:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Autodesk.AutoCAD.Runtime.Exception: eNoDatabase
Distinguished Contributor
RolandF
Posts: 172
Registered: ‎11-19-2003
Message 2 of 3 (242 Views)

Re: How to insert a blockreference with attibutes?

11-28-2005 06:03 AM in reply to: tangferry


This is what i am using, hope it helps. The original code is from Zhongwei,
Tony Tanzillo helped us ;-)


///
color=#808080 size=2><summary>


///
Inserts all attributreferences


///
</summary>


///
<param name="blkRef">
color=#008000 size=2>Blockreference to append the attributes

color=#808080 size=2></param>


public
color=#0000ff size=2>static

size=2>void
InsertBlockAttibuteRef(
color=#008080 size=2>BlockReference
blkRef)


{


Database dbCurrent =
HostApplicationServices
size=2>.WorkingDatabase;


Autodesk.AutoCAD.DatabaseServices.


size=2>TransactionManager
tm =
dbCurrent.TransactionManager;


using (
color=#008080 size=2>Transaction
tr =
tm.StartTransaction())


{


BlockTableRecord
btAttRec = (
BlockTableRecord
size=2>)tr.GetObject(blkRef.BlockTableRecord,

size=2>OpenMode
.ForRead);


foreach (
color=#008080 size=2>ObjectId
idAtt
color=#0000ff size=2>in
btAttRec)


{


Entity ent =
(
Entity
size=2>)tr.GetObject(idAtt,

size=2>OpenMode
.ForRead);


if (ent
color=#0000ff size=2>is

size=2>AttributeDefinition
)


{


AttributeDefinition
attDef = (
AttributeDefinition
size=2>)ent;


AttributeReference
attRef =
new
color=#008080 size=2>AttributeReference
();


attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);


ObjectId idTemp =
blkRef.AttributeCollection.AppendAttribute(attRef);


tr.AddNewlyCreatedDBObject(attRef,


size=2>true
);


}


}


tr.Commit();


}


}


///
<summary>


///
Inserts all attributreferences


///
</summary>


///
<param name="blkRef">
color=#008000 size=2>Blockreference to append the attributes

color=#808080 size=2></param>


///
<param name="tr">
color=#008000 size=2>Transaction

size=2></param>


public
color=#0000ff size=2>static

size=2>void
InsertBlockAttibuteRef(
color=#008080 size=2>BlockReference
blkRef,
color=#008080 size=2>Transaction
tr)


{


BlockTableRecord
btAttRec = (
BlockTableRecord
size=2>)tr.GetObject(blkRef.BlockTableRecord,

size=2>OpenMode
.ForRead);


foreach (
color=#008080 size=2>ObjectId
idAtt
color=#0000ff size=2>in
btAttRec)


{


Entity ent =
(
Entity
size=2>)tr.GetObject(idAtt,

size=2>OpenMode
.ForRead);


if (ent
color=#0000ff size=2>is

size=2>AttributeDefinition
)


{


AttributeDefinition
attDef = (
AttributeDefinition
size=2>)ent;


AttributeReference
attRef =
new
color=#008080 size=2>AttributeReference
();


attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);


ObjectId idTemp =
blkRef.AttributeCollection.AppendAttribute(attRef);


tr.AddNewlyCreatedDBObject(attRef,


size=2>true
);


}


}


}


///
<summary>


///
Inserts all attributreferences


///
</summary>


///
<param name="blkRef">
color=#008000 size=2>Blockreference to append the attributes

color=#808080 size=2></param>


///
<param
name="strAttributeText">
The textstring for
all attributes
</param>


size=2>

public
color=#0000ff size=2>static

size=2>void
InsertBlockAttibuteRef(
color=#008080 size=2>BlockReference
blkRef,
color=#0000ff size=2>string
strAttributeText)


{


Database dbCurrent =
HostApplicationServices
size=2>.WorkingDatabase;


Autodesk.AutoCAD.DatabaseServices.


size=2>TransactionManager
tm =
dbCurrent.TransactionManager;


using (
color=#008080 size=2>Transaction
tr =
tm.StartTransaction())


{


BlockTableRecord
btAttRec = (
BlockTableRecord
size=2>)tr.GetObject(blkRef.BlockTableRecord,

size=2>OpenMode
.ForRead);


foreach (
color=#008080 size=2>ObjectId
idAtt
color=#0000ff size=2>in
btAttRec)


{


Entity ent =
(
Entity
size=2>)tr.GetObject(idAtt,

size=2>OpenMode
.ForRead);


if (ent
color=#0000ff size=2>is

size=2>AttributeDefinition
)


{


AttributeDefinition
attDef = (
AttributeDefinition
size=2>)ent;


AttributeReference
attRef =
new
color=#008080 size=2>AttributeReference
();


attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);


attRef.TextString = strAttributeText;


ObjectId idTemp =
blkRef.AttributeCollection.AppendAttribute(attRef);


tr.AddNewlyCreatedDBObject(attRef,


size=2>true
);


}


}


tr.Commit();


}


}


///
<summary>


///
Inserts all attributreferences


///
</summary>


///
<param name="blkRef">
color=#008000 size=2>Blockreference to append the attributes

color=#808080 size=2></param>


///
<param
name="strAttributeText">
The textstring for
all attributes
</param>


size=2>

///
<param name="tr">
color=#008000 size=2>Transaction

size=2></param>


public
color=#0000ff size=2>static

size=2>void
InsertBlockAttibuteRef(
color=#008080 size=2>BlockReference
blkRef,
color=#0000ff size=2>string
strAttributeText,
color=#008080 size=2>Transaction
tr)


{


BlockTableRecord
btAttRec = (
BlockTableRecord
size=2>)tr.GetObject(blkRef.BlockTableRecord,

size=2>OpenMode
.ForRead);


foreach (
color=#008080 size=2>ObjectId
idAtt
color=#0000ff size=2>in
btAttRec)


{


Entity ent =
(
Entity
size=2>)tr.GetObject(idAtt,

size=2>OpenMode
.ForRead);


if (ent
color=#0000ff size=2>is

size=2>AttributeDefinition
)


{


AttributeDefinition
attDef = (
AttributeDefinition
size=2>)ent;


AttributeReference
attRef =
new
color=#008080 size=2>AttributeReference
();


attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);


attRef.TextString = strAttributeText;


ObjectId idTemp =
blkRef.AttributeCollection.AppendAttribute(attRef);


tr.AddNewlyCreatedDBObject(attRef,


size=2>true
);


}


}


}


///
<summary>


///
Inserts all attributreferences


///
</summary>


///
<param name="blkRef">
color=#008000 size=2>Blockreference to append the attributes

color=#808080 size=2></param>


///
<param
name="strAttributeTag">
The tag to insert
the
<paramref
name="strAttributeText"/></param>


///
<param
name="strAttributeText">
The textstring for
<paramref
name="strAttributeTag"/></param>


public
color=#0000ff size=2>static

size=2>void
InsertBlockAttibuteRef(
color=#008080 size=2>BlockReference
blkRef,
color=#0000ff size=2>string
strAttributeTag,
color=#0000ff size=2>string
strAttributeText)


{


Database dbCurrent =
HostApplicationServices
size=2>.WorkingDatabase;


Autodesk.AutoCAD.DatabaseServices.


size=2>TransactionManager
tm =
dbCurrent.TransactionManager;


using (
color=#008080 size=2>Transaction
tr =
tm.StartTransaction())


{


BlockTableRecord
btAttRec = (
BlockTableRecord
size=2>)tr.GetObject(blkRef.BlockTableRecord,

size=2>OpenMode
.ForRead);


foreach (
color=#008080 size=2>ObjectId
idAtt
color=#0000ff size=2>in
btAttRec)


{


Entity ent =
(
Entity
size=2>)tr.GetObject(idAtt,

size=2>OpenMode
.ForRead);


if (ent
color=#0000ff size=2>is

size=2>AttributeDefinition
)


{


AttributeDefinition
attDef = (
AttributeDefinition
size=2>)ent;


AttributeReference
attRef =
new
color=#008080 size=2>AttributeReference
();


attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);


if (attRef.Tag ==
strAttributeTag)


attRef.TextString = strAttributeText;


ObjectId idTemp =
blkRef.AttributeCollection.AppendAttribute(attRef);


tr.AddNewlyCreatedDBObject(attRef,


size=2>true
);


}


}


tr.Commit();


}


}


///
<summary>


///
Inserts all attributreferences


///
</summary>


///
<param name="blkRef">
color=#008000 size=2>Blockreference to append the attributes

color=#808080 size=2></param>


///
<param
name="strAttributeTag">
The tag to insert
the
<paramref
name="strAttributeText"/></param>


///
<param
name="strAttributeText">
The textstring for
<paramref
name="strAttributeTag"/></param>


///
<param name="tr">
color=#008000 size=2>Transacton

size=2></param>


public
color=#0000ff size=2>static

size=2>void
InsertBlockAttibuteRef(
color=#008080 size=2>BlockReference
blkRef,
color=#0000ff size=2>string
strAttributeTag,
color=#0000ff size=2>string
strAttributeText,
color=#008080 size=2>Transaction
tr)


{


BlockTableRecord
btAttRec = (
BlockTableRecord
size=2>)tr.GetObject(blkRef.BlockTableRecord,

size=2>OpenMode
.ForRead);


foreach (
color=#008080 size=2>ObjectId
idAtt
color=#0000ff size=2>in
btAttRec)


{


Entity ent =
(
Entity
size=2>)tr.GetObject(idAtt,

size=2>OpenMode
.ForRead);


if (ent
color=#0000ff size=2>is

size=2>AttributeDefinition
)


{


AttributeDefinition
attDef = (
AttributeDefinition
size=2>)ent;


AttributeReference
attRef =
new
color=#008080 size=2>AttributeReference
();


attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);


if (attRef.Tag ==
strAttributeTag)


attRef.TextString = strAttributeText;


ObjectId idTemp =
blkRef.AttributeCollection.AppendAttribute(attRef);


tr.AddNewlyCreatedDBObject(attRef,


size=2>true
);


}


}


}



--
Roland Feletic

 

I want to  insert a
blockreference with attibutes.
The following are the codes:
public class
Class1
    {
       
[CommandMethod("INSRTBLK")]
        public
void InsertBlk()
       
{
            Database
curDb =
HostApplicationServices.WorkingDatabase;
           
Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
           
PromptResult stringRes = ed.GetString("\nEnter Block Name:
");
            string
blkName =
stringRes.StringResult;
           
if (stringRes.Status != PromptStatus.OK && blkName.Length ==
0)
           
{
               
ed.WriteMessage("\nInvalid block
name.");
               
return;
           
}
            DBTrans
tm =
curDb.TransactionManager;
           
using (Transaction trans =
tm.StartTransaction())
           
{
               
BlockTable blkTable = (BlockTable)tm.GetObject(curDb.BlockTableId,
OpenMode.ForRead);
               
if (blkTable.Has(blkName) ==
false)
               
{
                   
ed.WriteMessage(string.Format("\nBlock definition {0} not found. ",
blkName));
                   
return;
               
}
               
ObjectId blkId =
blkTable[blkName];
               
BlockTableRecord blkTableRecord =
(BlockTableRecord)tm.GetObject(blkTable[BlockTableRecord.ModelSpace],
OpenMode.ForWrite);
               
PromptPointOptions pointOpt = new PromptPointOptions("\nPick insertion point:
");
               
pointOpt.AllowNone =
false;
               
PromptPointResult pointRes =
ed.GetPoint(pointOpt);
               
BlockReference insrtObj = new BlockReference(pointRes.Value,
blkId);
               
BlockTableRecord blkDefRecord = (BlockTableRecord)tm.GetObject(blkId,
OpenMode.ForRead);
               
if
(blkDefRecord.HasAttributeDefinitions)
               
{
                   
foreach (ObjectId defId in
blkDefRecord)
                   
{
                       
AttributeDefinition attDef = tm.GetObject(defId, OpenMode.ForRead) as
AttributeDefinition;
                       
if (attDef != null && attDef.Constant ==
false)
                       
{
                           
AttributeReference att = new
AttributeReference();
                           
att.SetPropertiesFrom(attDef);
                           
att.Invisible =
attDef.Invisible;
                           
Point3d basePt =
attDef.Position;
                           
basePt = basePt +
insrtObj.Position.GetAsVector();
                           
att.Position =
basePt;
                           
att.Height =
attDef.Height;
                           
att.Rotation =
attDef.Rotation;
                           
string tagPrompt =
attDef.Tag;
                           
att.Tag =
tagPrompt;
                           
tagPrompt =
attDef.Prompt;
                           
ed.WriteMessage(string.Format("{0}{1}", "\n",
tagPrompt));
                           
att.FieldLength =
25;
                           
att.TextString = "This is a
test";
                           
insrtObj.AttributeCollection.AppendAttribute(att);
                       
}
                   
}
               
}
               
blkTableRecord.AppendEntity(insrtObj);
               
tm.AddNewlyCreatedDBObject(insrtObj,
true);
               
trans.Commit();
           
}           

        }
   
}


But when i run it in the AutoCAD,I get the
errors:
System.Reflection.TargetInvocationException: Exception has been
thrown by the target of an invocation. --->
Autodesk.AutoCAD.Runtime.Exception: eNoDatabase
Distinguished Contributor
RolandF
Posts: 172
Registered: ‎11-19-2003
Message 3 of 3 (242 Views)

Re: How to insert a blockreference with attibutes?

11-28-2005 06:03 AM in reply to: tangferry
I use following code. The code i used is from Zhongwei Xia, with a little
help from Tony Tanzillo.

///

/// Inserts all attributreferences

///


/// Blockreference to append the attributes

public static void InsertBlockAttibuteRef(BlockReference blkRef)

{

Database dbCurrent = HostApplicationServices.WorkingDatabase;

Autodesk.AutoCAD.DatabaseServices.TransactionManager tm =
dbCurrent.TransactionManager;

using (Transaction tr = tm.StartTransaction())

{

BlockTableRecord btAttRec =
(BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);

foreach (ObjectId idAtt in btAttRec)

{

Entity ent = (Entity)tr.GetObject(idAtt, OpenMode.ForRead);

if (ent is AttributeDefinition)

{

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();

attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);

ObjectId idTemp = blkRef.AttributeCollection.AppendAttribute(attRef);

tr.AddNewlyCreatedDBObject(attRef, true);

}

}

tr.Commit();

}

}

///

/// Inserts all attributreferences

///


/// Blockreference to append the attributes

/// Transaction

public static void InsertBlockAttibuteRef(BlockReference blkRef, Transaction
tr)

{

BlockTableRecord btAttRec =
(BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);

foreach (ObjectId idAtt in btAttRec)

{

Entity ent = (Entity)tr.GetObject(idAtt, OpenMode.ForRead);

if (ent is AttributeDefinition)

{

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();

attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);

ObjectId idTemp = blkRef.AttributeCollection.AppendAttribute(attRef);

tr.AddNewlyCreatedDBObject(attRef, true);

}

}

}

///

/// Inserts all attributreferences

///


/// Blockreference to append the attributes

/// The textstring for all attributes

public static void InsertBlockAttibuteRef(BlockReference blkRef, string
strAttributeText)

{

Database dbCurrent = HostApplicationServices.WorkingDatabase;

Autodesk.AutoCAD.DatabaseServices.TransactionManager tm =
dbCurrent.TransactionManager;

using (Transaction tr = tm.StartTransaction())

{

BlockTableRecord btAttRec =
(BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);

foreach (ObjectId idAtt in btAttRec)

{

Entity ent = (Entity)tr.GetObject(idAtt, OpenMode.ForRead);

if (ent is AttributeDefinition)

{

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();

attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);

attRef.TextString = strAttributeText;

ObjectId idTemp = blkRef.AttributeCollection.AppendAttribute(attRef);

tr.AddNewlyCreatedDBObject(attRef, true);

}

}

tr.Commit();

}

}

///

/// Inserts all attributreferences

///


/// Blockreference to append the attributes

/// The textstring for all attributes

/// Transaction

public static void InsertBlockAttibuteRef(BlockReference blkRef, string
strAttributeText, Transaction tr)

{

BlockTableRecord btAttRec =
(BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);

foreach (ObjectId idAtt in btAttRec)

{

Entity ent = (Entity)tr.GetObject(idAtt, OpenMode.ForRead);

if (ent is AttributeDefinition)

{

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();

attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);

attRef.TextString = strAttributeText;

ObjectId idTemp = blkRef.AttributeCollection.AppendAttribute(attRef);

tr.AddNewlyCreatedDBObject(attRef, true);

}

}

}

///

/// Inserts all attributreferences

///


/// Blockreference to append the attributes

/// The tag to insert the
name="strAttributeText"/>

/// The textstring for
name="strAttributeTag"/>

public static void InsertBlockAttibuteRef(BlockReference blkRef, string
strAttributeTag, string strAttributeText)

{

Database dbCurrent = HostApplicationServices.WorkingDatabase;

Autodesk.AutoCAD.DatabaseServices.TransactionManager tm =
dbCurrent.TransactionManager;

using (Transaction tr = tm.StartTransaction())

{

BlockTableRecord btAttRec =
(BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);

foreach (ObjectId idAtt in btAttRec)

{

Entity ent = (Entity)tr.GetObject(idAtt, OpenMode.ForRead);

if (ent is AttributeDefinition)

{

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();

attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);

if (attRef.Tag == strAttributeTag)

attRef.TextString = strAttributeText;

ObjectId idTemp = blkRef.AttributeCollection.AppendAttribute(attRef);

tr.AddNewlyCreatedDBObject(attRef, true);

}

}

tr.Commit();

}

}

///

/// Inserts all attributreferences

///


/// Blockreference to append the attributes

/// The tag to insert the
name="strAttributeText"/>

/// The textstring for
name="strAttributeTag"/>

/// Transacton

public static void InsertBlockAttibuteRef(BlockReference blkRef, string
strAttributeTag, string strAttributeText, Transaction tr)

{

BlockTableRecord btAttRec =
(BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);

foreach (ObjectId idAtt in btAttRec)

{

Entity ent = (Entity)tr.GetObject(idAtt, OpenMode.ForRead);

if (ent is AttributeDefinition)

{

AttributeDefinition attDef = (AttributeDefinition)ent;

AttributeReference attRef = new AttributeReference();

attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);

if (attRef.Tag == strAttributeTag)

attRef.TextString = strAttributeText;

ObjectId idTemp = blkRef.AttributeCollection.AppendAttribute(attRef);

tr.AddNewlyCreatedDBObject(attRef, true);

}

}

}


--
Roland Feletic
schrieb im Newsbeitrag news:5023173@discussion.autodesk.com...
I want to insert a blockreference with attibutes.
The following are the codes:
public class Class1
{
[CommandMethod("INSRTBLK")]
public void InsertBlk()
{
Database curDb = HostApplicationServices.WorkingDatabase;
Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
PromptResult stringRes = ed.GetString("\nEnter Block Name: ");
string blkName = stringRes.StringResult;
if (stringRes.Status != PromptStatus.OK && blkName.Length == 0)
{
ed.WriteMessage("\nInvalid block name.");
return;
}
DBTrans tm = curDb.TransactionManager;
using (Transaction trans = tm.StartTransaction())
{
BlockTable blkTable =
(BlockTable)tm.GetObject(curDb.BlockTableId, OpenMode.ForRead);
if (blkTable.Has(blkName) == false)
{
ed.WriteMessage(string.Format("\nBlock definition {0}
not found. ", blkName));
return;
}
ObjectId blkId = blkTable[blkName];
BlockTableRecord blkTableRecord =
(BlockTableRecord)tm.GetObject(blkTable[BlockTableRecord.ModelSpace],
OpenMode.ForWrite);
PromptPointOptions pointOpt = new PromptPointOptions("\nPick
insertion point: ");
pointOpt.AllowNone = false;
PromptPointResult pointRes = ed.GetPoint(pointOpt);
BlockReference insrtObj = new BlockReference(pointRes.Value,
blkId);
BlockTableRecord blkDefRecord =
(BlockTableRecord)tm.GetObject(blkId, OpenMode.ForRead);
if (blkDefRecord.HasAttributeDefinitions)
{
foreach (ObjectId defId in blkDefRecord)
{
AttributeDefinition attDef = tm.GetObject(defId,
OpenMode.ForRead) as AttributeDefinition;
if (attDef != null && attDef.Constant == false)
{
AttributeReference att = new
AttributeReference();
att.SetPropertiesFrom(attDef);
att.Invisible = attDef.Invisible;
Point3d basePt = attDef.Position;
basePt = basePt +
insrtObj.Position.GetAsVector();
att.Position = basePt;
att.Height = attDef.Height;
att.Rotation = attDef.Rotation;
string tagPrompt = attDef.Tag;
att.Tag = tagPrompt;
tagPrompt = attDef.Prompt;
ed.WriteMessage(string.Format("{0}{1}", "\n",
tagPrompt));
att.FieldLength = 25;
att.TextString = "This is a test";
insrtObj.AttributeCollection.AppendAttribute(att);
}
}
}
blkTableRecord.AppendEntity(insrtObj);
tm.AddNewlyCreatedDBObject(insrtObj, true);
trans.Commit();
}
}
}


But when i run it in the AutoCAD,I get the errors:
System.Reflection.TargetInvocationException: Exception has been thrown by
the target of an invocation. ---> Autodesk.AutoCAD.Runtime.Exception:
eNoDatabase

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community