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

AttributeDefinition's position property doesn't work

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Miralkong
699 Views, 4 Replies

AttributeDefinition's position property doesn't work

Hi, guys. I'm trying to create a block with attribute with it. But no matter how I adjust the position property of attributedefinition, the attribute still be insert in the origin point(Like in the following code, I assigned (12,0,0) as it's insert point, but still shows in (0,0,0)). Could anyone tell me how to adjust the position of the attribute in a block. Thanks a lot! The following the the code:

0.png
public void InsertingBlockWithAnAttribute() { Document acDoc = Application.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; ObjectId blkRecId = ObjectId.Null; if (!acBlkTbl.Has("ZH_Elevation")) { BlockTableRecord elevatioanRecord = new BlockTableRecord(); elevatioanRecord.Name = "ZH_Elevation"; Point3d elevationMarkInsertPt = new Point3d(0, 0, 0); elevatioanRecord.Origin = elevationMarkInsertPt; using (Polyline acPolyline = new Polyline()) { acPolyline.AddVertexAt(acPolyline.NumberOfVertices, new Point2d(-3, 3), 0, 0, 0); acPolyline.AddVertexAt(acPolyline.NumberOfVertices, new Point2d(0, 0), 0, 0, 0); acPolyline.AddVertexAt(acPolyline.NumberOfVertices, new Point2d(3, 3), 0, 0, 0); acPolyline.AddVertexAt(acPolyline.NumberOfVertices, new Point2d(-12, 3), 0, 0, 0); elevatioanRecord.AppendEntity(acPolyline); using (AttributeDefinition acAttDef = new AttributeDefinition()) { acAttDef.Position = new Point3d(-12, 0, 0); acAttDef.Prompt = "Elevation"; acAttDef.Tag = "ElevationMark"; acAttDef.TextString = elevationMarkInsertPt.Y == 0 ? "%%P" + elevationMarkInsertPt.Y.ToString("f3") : elevationMarkInsertPt.Y.ToString("f3"); acAttDef.Height = 3; acAttDef.Justify = AttachmentPoint.BottomLeft; acAttDef.Verifiable = false; elevatioanRecord.AppendEntity(acAttDef); acBlkTbl.UpgradeOpen(); acBlkTbl.Add(elevatioanRecord); acTrans.AddNewlyCreatedDBObject(elevatioanRecord, true); } } blkRecId = elevatioanRecord.Id; } else { blkRecId = acBlkTbl["ZH_Elevation"]; } if (blkRecId != null) { BlockTableRecord acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord; using (BlockReference acBlkRef = new BlockReference(new Point3d(0, 0, 0), blkRecId)) { BlockTableRecord acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; acCurSpaceBlkTblRec.AppendEntity(acBlkRef); acTrans.AddNewlyCreatedDBObject(acBlkRef, true); if (acBlkTblRec.HasAttributeDefinitions) { foreach (ObjectId objectId in acBlkTblRec) { DBObject dbObj = acTrans.GetObject(objectId, OpenMode.ForRead) as DBObject; if (dbObj is AttributeDefinition) { AttributeDefinition attTemp = dbObj as AttributeDefinition; if (!attTemp.Constant) { using (AttributeReference attTempRef = new AttributeReference()) { attTempRef.SetAttributeFromBlock(attTemp, acBlkRef.BlockTransform); Vector3d a = new Point3d(0, 0, 0).GetVectorTo(new Point3d(-12, 0, 0)); attTempRef.Position = attTemp.Position.TransformBy(Matrix3d.Displacement(a)); attTempRef.TextString = attTempRef.Position.Y.ToString("f3"); acBlkRef.AttributeCollection.AppendAttribute(attTempRef); acTrans.AddNewlyCreatedDBObject(attTempRef, true); } } } } } } } acTrans.Commit(); } }
4 REPLIES 4
Message 2 of 5
_gile
in reply to: Miralkong

Hi,

 

While AttachmentPoint is different from AttachmentPoint.BaseLeft, you have to specify the AlignmentPoint property.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 5
Miralkong
in reply to: _gile

Hi, thanks. That works.

But I'm still wondering that what's the function of acAttDef.Position. From the developer's guide, it seems that it controls the location point of attribute, however I can't find any difference with several parameters set to it and even if I comment the code, it makes no different and no error message.

Message 4 of 5
_gile
in reply to: Miralkong

When DBText.Justify == AttachmentPoint.BaseLeft the Position property is used and AlignmentPoint is ignored.

In all other cases, AlignmentPoint is used and the setting of Position property is ignored , but AutoCAD cinternally sets this property to be at the BaseLeft position of the text.

 

Liddle center justified text:

Justify.png



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 5
Miralkong
in reply to: _gile

I see, thanks a lot.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report