.NET

Reply
Valued Contributor
HelloWorlddd
Posts: 87
Registered: ‎05-03-2013
Message 1 of 7 (452 Views)
Accepted Solution

BlockReference and AttributeDefinition

452 Views, 6 Replies
03-20-2014 09:28 AM

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.

xx.PNG

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

*Expert Elite*
_gile
Posts: 2,084
Registered: ‎04-29-2006
Message 2 of 7 (441 Views)

Re : BlockReference and AttributeDefinition

03-20-2014 10:37 AM in reply to: HelloWorlddd

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...

Gilles Chanteau
Valued Mentor
AubelecBE
Posts: 391
Registered: ‎01-27-2010
Message 3 of 7 (415 Views)

Re : BlockReference and AttributeDefinition

03-20-2014 05:55 PM in reply to: _gile

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

 

 

 

 

 

 

 

 

 

 

 

Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 4 of 7 (404 Views)

Re: BlockReference and AttributeDefinition

03-21-2014 12:40 AM in reply to: HelloWorlddd

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.

 

Capture.JPG

Valued Mentor
AubelecBE
Posts: 391
Registered: ‎01-27-2010
Message 5 of 7 (384 Views)

Re: BlockReference and AttributeDefinition

03-21-2014 08:27 AM in reply to: mzakiralam

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

 

 

New Member
kanatrix
Posts: 1
Registered: ‎11-29-2010
Message 6 of 7 (336 Views)

Re: BlockReference and AttributeDefinition

03-24-2014 11:17 AM in reply to: AubelecBE


//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..

Valued Contributor
HelloWorlddd
Posts: 87
Registered: ‎05-03-2013
Message 7 of 7 (281 Views)

Re: BlockReference and AttributeDefinition

04-05-2014 10:19 AM in reply to: kanatrix

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

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 or visit the Installation and Licensing Forum to get help installing your software.