What is happening here?
I created a BlockReference object that contains a AttributeDefinition object, and then I set AttributeDefinition object Height = 2.5714
However, after drawing, the Height of AttributeDefinition is equal to 2.5
I think I'm a little confused, In fact I just want the height of the text is equal to 1
Below is source code.
using System; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; [assembly: CommandClass(typeof(AutocadCommandCollection.AddAttributeBlock))] namespace AutocadCommandCollection { class AddAttributeBlock { [CommandMethod("AD")] public void AddBlock() { Database acCurDb = HostApplicationServices.WorkingDatabase; using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite) as BlockTable; LayerTable layerTableObj = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable; TextStyleTable textStyleTableObj = acTrans.GetObject(acCurDb.TextStyleTableId, OpenMode.ForRead) as TextStyleTable; BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; BlockTableRecord newBlkTblRec = new BlockTableRecord(); newBlkTblRec.Name = "tstrmX"; Line line1 = new Line(new Point3d(-1.75, 0, 0), new Point3d(0, 1.75, 0)); Line line2 = new Line(new Point3d(0, 1.75, 0), new Point3d(1.75, 0, 0)); Line line3 = new Line(new Point3d(1.75, 0, 0), new Point3d(0, -1.75, 0)); Line line4 = new Line(new Point3d(0, -1.75, 0), new Point3d(-1.75, 0, 0)); if (layerTableObj.Has("0") == true) { line1.Layer = "0"; line2.Layer = "0"; line3.Layer = "0"; line4.Layer = "0"; } AttributeDefinition attDef = new AttributeDefinition(); attDef.Constant = false; attDef.Tag = "TYPE"; attDef.Prompt = "STREAM #"; attDef.TextString = "51"; attDef.Height = 2.5714; attDef.WidthFactor = 0.9694; attDef.Justify = AttachmentPoint.MiddleCenter; attDef.AlignmentPoint = new Point3d(0, 0, 0); if (layerTableObj.Has("0") == true) { attDef.Layer = "0"; } if (textStyleTableObj.Has("ROMANS")) { attDef.TextStyleId = textStyleTableObj["ROMANS"]; } newBlkTblRec.AppendEntity(line1); newBlkTblRec.AppendEntity(line2); newBlkTblRec.AppendEntity(line3); newBlkTblRec.AppendEntity(line4); newBlkTblRec.AppendEntity(attDef); ObjectId acBlkTblId = acBlkTbl.Add(newBlkTblRec); acTrans.AddNewlyCreatedDBObject(newBlkTblRec, true); BlockReference newBlkRef = new BlockReference(Point3d.Origin, acBlkTblId); if (layerTableObj.Has("FL") == true) { newBlkRef.Layer = "FL"; } acBlkTblRec.AppendEntity(newBlkRef); acTrans.AddNewlyCreatedDBObject(newBlkRef, true); foreach (ObjectId acObjId in newBlkTblRec) { if (acObjId.ObjectClass.Equals(RXClass.GetClass(typeof(AttributeDefinition)))) { AttributeDefinition acObjDef = acTrans.GetObject(acObjId, OpenMode.ForRead) as AttributeDefinition; AttributeReference attRef = new AttributeReference(acObjDef.Position, acObjDef.TextString, acObjDef.Tag, new ObjectId()); newBlkRef.AttributeCollection.AppendAttribute(attRef); } } acTrans.Commit(); } } } }
Is anyone can help me, very thanks.
Solved! Go to Solution.
You say :
"I created a BlockReference object that contains a AttributeDefinition object, and then I set AttributeDefinition object Height = 2.5714
[...]
I think I'm a little confused, In fact I just want the height of the text is equal to 1"
If so, set AttributeDefinition object Height to 1...
hi. for create the attributreference use :
AttributReference attRef = new AttributRefence();
AttRef.SetAttributeFromBlock(acObjDef, newBlkTblRec.BlockTransform);
and you have to check the unit of your drawing..
example : NewDefBloc.Units = UnitsValue.Centimeters --> your block use centimer so if you drawing is cm the scale is 1
but if drawing is meter, the scale is not 1 but 100 or 0.01 depend of your bloc.
example of my function for calculate this. (all my block use unit : cm.) :
'**************************************************************************** '*** Function de Calcul de l'échelle d'insertion d'un bloc ****************** '**************************************************************************** ' -- ENTREE : ' DefBloc : La définition du bloc ' SORTI ' l'unité multiplicateur de l'échelle du bloc ' (sert à compenser la mise à l'échelle automatique d'autocad) '**************************************************************************** Public Function CalculEChelleBloc(ByRef DefBloc As BlockTableRecord) As Double Dim unite As Double Dim obj As Object = acApp.GetSystemVariable("INSUNITS") Select Case System.Convert.ToInt16(obj) Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Millimeters Select Case DefBloc.Units Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Millimeters : unite = 1 'mm Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Centimeters : unite = 10 'cm Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Decimeters : unite = 100 'cm Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Meters : unite = 1000 'm Case Else : unite = 1 End Select Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Centimeters Select Case DefBloc.Units Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Millimeters : unite = 0.1 'mm Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Centimeters : unite = 1 'cm Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Decimeters : unite = 10 'cm Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Meters : unite = 100 'm Case Else : unite = 1 End Select Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Decimeters Select Case DefBloc.Units Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Millimeters : unite = 0.01 'mm Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Centimeters : unite = 0.1 'cm Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Decimeters : unite = 1 'cm Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Meters : unite = 10 'm Case Else : unite = 1 End Select Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Meters Select Case DefBloc.Units Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Millimeters : unite = 0.001 'mm Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Centimeters : unite = 0.01 'cm Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Decimeters : unite = 0.1 'cm Case Autodesk.AutoCAD.DatabaseServices.UnitsValue.Meters : unite = 1 'm Case Else : unite = 1 End Select Case Else : unite = 1 End Select Return unite End Function
sorry i use vb.net
and use it :
Dim unit As Double = CalculEChelleBloc(DefBloc)
Dim Echelle As Scale3d
Echelle = New Scale3d(unit, unit, unit)
RefBlock.ScaleFactors = Echelle
Hi , I think this is occuring due to precision of units. Can you check units and probably it is like below? if you want 2.5714 then precision should be 0.0000.
when you insert a blockreference in your drawing, acad check :
1 - your current units of drawing,
2 - the unit of your block
3 - your setting in the options.
for 1 - see the command UI
for 2 - it is when you create the defblock. (see my previous post)
for 3 - see in OPTIONS the tab pref user. (sr i am french so i dont really know the good translation for the good name in english autocad )
you have to
1 - set the unit for your DefBlock
2 - set the unit of your drawing
3 - set the correct convert in the option (if you set NoUnits in your DefBlock)
4 - Insert your block with the correct scale and the attributdef/ref with sscale set to 1
//I'm from Brazil
//I'm still having problem
//need help
//can someone help me?
//insert a block, and in the sequel want the data of this block,
//and then delete this block
//as the example in the main code, just below ..
//I have these questions that are commented no Main method >>???????
//Main method
//===================================================================================================
[CommandMethod("Blockk")]
public void blockk()
{
Document doc = Host.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
string[] data = new string[3];
data[0] = "data 1";
data[1] = "data 2";
data[2] = "data 3";
BlockReference db = InsertBlockFromFile_(doc, @"C:\wbc\orienta.dwg", new Point3d(50, 50, 50), Math.PI * 0.25,
"wbc_insert", data, 30, 30, 1, 7, true, null, "wbcd");
//??????? return..>>> Example
//??????? db.insertionPoint
//??????? db.angle
//??????? db.handle
//??????? db.Xproprierts
//??????? db.Atrib
//??????? db.erase
}
//Complementary methods
//===============================================================================================================================
public static BlockReference InsertBlockFromFile_(Document doc, string DrawingFile, Point3d Pinsert, double Angle,
string Layer, string[] DadosExtendidos = null, double escalax = 1.0, double escalay = 1.0, double escalaz = 1.0, short ColorLayer = 7,
bool ModelSpace = true, string[] Atributos = null, string RegAplicacao = "wbc")
{
if (!System.IO.File.Exists(DrawingFile))
{
MessageBox.Show("Drawing not found\n" + DrawingFile);
return null;
}
string sourceFileName = DrawingFile;
string Blockname = System.IO.Path.GetFileNameWithoutExtension(sourceFileName);
try
{
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
Database db = new Database(false, false);
db.ReadDwgFile(DrawingFile, System.IO.FileShare.Read, true, null);
ObjectId BlkId = doc.Database.Insert(Blockname, db, true); // ISL: Small fix, use correct block name
tr.Commit();
}
}
catch (IOException ioe)
{
MessageBox.Show(ioe.Message);
return null;
}
return AddBlockTest_(doc, Blockname, Pinsert, Angle, Layer, DadosExtendidos, escalax, escalay, escalaz, ColorLayer, Atributos, RegAplicacao);
}
private static BlockReference AddBlockTest_(Document doc, string blockName, Point3d Pinsert, double Angle,
string Layer, string[] DadosExtendidos = null, double escalax = 1.0, double escalay = 1.0, double escalaz = 1.0,
short ColorLayer = 7, string[] Atributos = null, string Regaplicacao = "wbc")
{
Database db = doc.Database;
using (Transaction myT = db.TransactionManager.StartTransaction())
{
//Get the block definition "Check".
BlockTable bt =
db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord blockDef =
bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
BlockTableRecord ms = (BlockTableRecord)myT.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
//Create new BlockReference, and link it to our block definition
using (BlockReference blockRef =
new BlockReference(Pinsert, blockDef.ObjectId))
{
blockRef.Rotation = Angle;
blockRef.ScaleFactors = new Scale3d(escalax, escalay, escalaz);
//Add the block reference to modelspace
ms.AppendEntity(blockRef);
myT.AddNewlyCreatedDBObject(blockRef, true);
SetXData_(blockRef, Regaplicacao , DadosExtendidos, doc);
CreatLayer_(Layer, doc, ColorLayer);
blockRef.Layer = Layer;
}
//Our work here is done
myT.Commit();
}
return null;
}
static public void SetXData_(Entity bref, string nome, string[] data, Document doc, string VersaoEquipamento = "Without Version")
{
Database db = doc.Database;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
AddRegAppTableRecord_(nome, doc);
ResultBuffer rb = new ResultBuffer();
rb.Add(new TypedValue(1001, nome));
for (int i = 0; i < data.Length; i++)
{
rb.Add(new TypedValue(1000, data[i]));
}
rb.Add(new TypedValue(1000, VersaoEquipamento));
DateTime Wdata = DateTime.Now;
Wdata.ToShortDateString();
rb.Add(new TypedValue(1000, Wdata.ToString()));
rb.Add(new TypedValue(1000, VariaveisGlobais.Wusuario + "_" + VariaveisGlobais.NumeroSerie));
bref.XData = rb;
tr.Commit();
}
}
public static void CreatLayer_(string sLayerName, Document doc, short CorCamada = 7)
{
Document acDoc = doc;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Layer table for read
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
if (acLyrTbl.Has(sLayerName) == false)
{
Platform.DatabaseServices.LayerTableRecord acLyrTblRec = new LayerTableRecord();
// Assign the layer the ACI color 1 and a name
acLyrTblRec.Color = Platform.Colors.Color.FromColorIndex(Platform.Colors.ColorMethod.ByAci, CorCamada);
acLyrTblRec.Name = sLayerName;
// Upgrade the Layer table for write
acLyrTbl.UpgradeOpen();
// Append the new layer to the Layer table and the transaction
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
acTrans.Commit();
}
}
public static void AddRegAppTableRecord_(string regAppName, Document doc)
{
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
RegAppTable rat = (RegAppTable)tr.GetObject(doc.Database.RegAppTableId, OpenMode.ForRead, false);
if (!rat.Has(regAppName))
{
rat.UpgradeOpen();
RegAppTableRecord ratr = new RegAppTableRecord();
ratr.Name = regAppName;
rat.Add(ratr);
tr.AddNewlyCreatedDBObject(ratr, true);
}
tr.Commit..
I have solve this problem by clone.
If anyone need to read more, it can be find from here: http://forums.autodesk.com/t5/NET/Copy-Problems/td-p/4896188
Can't find what you're looking for? Ask the community or share your knowledge.