multiline text to attribute

multiline text to attribute

abhijit_patilJ9KUT
Explorer Explorer
320 Views
1 Reply
Message 1 of 2

multiline text to attribute

abhijit_patilJ9KUT
Explorer
Explorer

 

ObjectId styleId = db.Textstyle;

// Add an attribute to the block definition
AttributeDefinition attDef = new AttributeDefinition(attributeInsertionPoint, attributeValue, attributeName, attributeName, styleId);
attDef.Height = 2 * scale;
attDef.Position = attributeInsertionPoint;
attDef.HorizontalMode = TextHorizontalMode.TextCenter;
attDef.VerticalMode = TextVerticalMode.TextVerticalMid;
if (columnHeaderNames[count] == "REMARKS" || columnHeaderNames[count] == "DESCRIPTION")
{
	attDef.IsMTextAttributeDefinition = true;
	
	MText mText = new MText();
	mText.SetDatabaseDefaults();
	mText.Contents = attributeValue;
	mText.TextHeight = 2 * scale;
	mText.Location = attributeInsertionPoint;
	mText.Width = 42 * scale; // Adjust width as needed
	mText.Attachment = AttachmentPoint.MiddleCenter;
	
	//rowBlock.AppendEntity(mText);
	//ObjectId mTextId = rowBlock.AppendEntity(mText);
	
	attDef.MTextAttributeDefinition = mText;
	
	
	//mText.Dispose();
}



bt.UpgradeOpen();
//ObjectId rowBlockId = bt.Add(rowBlock);
ObjectId rowBlockId = ObjectId.Null;
try
{
	if(bt != null && bt.IsWriteEnabled && rowBlock.IsWriteEnabled)
	{
		rowBlockId = bt.Add(rowBlock);
	}
	
}
catch (System.Exception ex)
{
	ed.WriteMessage(ex.Message);
}

tr.AddNewlyCreatedDBObject(rowBlock, true);

// Insert the row block in the current space
BlockReference rowReference = new BlockReference(new Point3d(tableRecordInsertionPoint.X, distanceInY, 0), rowBlockId);
BlockTableRecord modelSpace = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
modelSpace.AppendEntity(rowReference);
tr.AddNewlyCreatedDBObject(rowReference, true);

// Iterate through the row block and its attributes to add them to the row reference
var attributeCount = 0;
foreach (ObjectId objId in rowBlock)
{
	DBObject obj = tr.GetObject(objId, OpenMode.ForRead) as DBObject;
	
	if (obj is AttributeDefinition)
	{
		AttributeDefinition attDef = obj as AttributeDefinition;
		
		if (!attDef.Constant)
		{
			// Create an AttributeReference based on the AttributeDefinition
			AttributeReference attRef = new AttributeReference();
			attRef.SetAttributeFromBlock(attDef, rowReference.BlockTransform);
			
			
			
			// Set the attribute's position relative to the block reference
			attRef.Position = attributeList[attributeCount];
			attRef.AlignmentPoint = attributeList[attributeCount];
			
			
			//// Add the attribute reference to the block reference
			rowReference.AttributeCollection.AppendAttribute(attRef);
			tr.AddNewlyCreatedDBObject(attRef, true);
			attributeCount++;
		}
		
		
	}
}


tr.Commit();

 



here when i add attribute defination to attribute reference my drawing is created and multiline text is also visible but when i save drawing my autocad is crashing

here rowBlock is blocktable record and bt is blocktable how to add multiine text to attributedefination value in attribute

0 Likes
321 Views
1 Reply
Reply (1)
Message 2 of 2

norman.yuan
Mentor
Mentor

Firstly, this forum is for ObjectRAX C++, while your question is .NET API topic, thus you should post here.

 

Secondly, please use the "</>" button to post code, so that the code shows more readable.

 

As for your code:

You did not show the code how the block definition (rowBlock) is created. More importantly, after the code of creating new attribute definition (attDef), there is no code to add it to the block definition (e.g. you should have this line after the addDef is created: rowBlock.AppendEntity(attDef), followed by adding attDef to the Transaction). That is, your code may have created a entity (Attribute Definition) in memory orphaned (not residing in database, nor disposed). This could be the reason for the crash when saving the drawing.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes