Replies: 4 comments 10 replies
-
I think you have to add the something like ...
blockRef.appendAttribute(attref)
if attdef.hasFields():
of = Db.Field(attdef.getField())
flag = Db.FieldCodeFlag( Db.FieldCodeFlag.kFieldCode | Db.FieldCodeFlag.kAddMarkers)
nf = Db.Field(of.getFieldCode(flag))
attref.setField(nf)
nf.evaluate() |
Beta Was this translation helpful? Give feedback.
-
Oh, I see blockRef.appendAttribute(attref)
if attdef.hasFields():
of = Db.Field(attdef.getField())
flag = Db.FieldCodeFlag(
Db.FieldCodeFlag.kFieldCode |
Db.FieldCodeFlag.kAddMarkers
)
fcode = of.getFieldCode(flag)
objcode = '%<\\_ObjId {}>%'.format(blockRef.objectId().asOldId())
fcode= fcode.replace('?BlockRefId',objcode)
#print(fcode)
nf = Db.Field(fcode)
attref.setField(nf)
# regen |
Beta Was this translation helpful? Give feedback.
-
I don't want to use Ax because my code tests several positions of the block before actually inserting it. I know this approach isn't ideal either, because the block needs to be added to the database before I can add attributes to it. Should I use My code: @pass_working_db
def get_block_reference(
btr_id_or_name: Db.ObjectId | str,
point: UPoint3d = Ge.Point3d(0, 0, 0),
rotation: float = 0.0,
scale: float = 1.0,
db: Db.Database = ...,
):
if isinstance(btr_id_or_name, str):
btr_id = get_block_by_name(btr_id_or_name, must_exists=False, db=db)
elif not isinstance(btr_id_or_name, Db.ObjectId):
raise TypeError("btr_id_or_name must be a string or Db.ObjectId")
point = point_3d(point)
block_ref = Db.BlockReference(point, btr_id)
block_ref.setScaleFactors(Ge.Scale3d(scale))
block_ref.setRotation(rotation)
db.addObject(block_ref)
block_def = Db.BlockTableRecord(btr_id)
mt = Ge.Matrix3d.translation(point.asVector())
mr = Ge.Matrix3d.rotation(rotation, Ge.Vector3d.kZAxis, Ge.Point3d.kOrigin)
ms = Ge.Matrix3d.scaling(scale, Ge.Point3d.kOrigin)
matrix = mt * mr * ms
if block_def.hasAttributeDefinitions():
for att_def_id in block_def.objectIds(Db.AttributeDefinition.desc()):
att_def = Db.AttributeDefinition(att_def_id)
if att_def.isConstant():
continue
att_ref = Db.AttributeReference()
att_ref.setOwnerId(block_ref.objectId())
att_ref.setPropertiesFrom(att_def)
att_ref.setInvisible(att_def.isInvisible())
att_ref.setPosition(att_def.position().transformBy(matrix))
att_ref.setHeight(att_def.height() * scale)
att_ref.setRotation(att_def.rotation() + rotation)
att_ref.setFieldLength(att_def.fieldLength())
att_ref.setHorizontalMode(att_def.horizontalMode())
att_ref.setVerticalMode(att_def.verticalMode())
att_ref.setTag(att_def.tag())
att_ref.setTextStyle(att_def.textStyle())
att_ref.setWidthFactor(att_def.widthFactor())
att_ref.setAlignmentPoint(att_def.alignmentPoint().transformBy(matrix))
block_ref.appendAttribute(att_ref)
if att_def.hasFields():
att_def_field = Db.Field(att_def.getField())
flag = Db.FieldCodeFlag(Db.FieldCodeFlag.kAddMarkers | Db.FieldCodeFlag.kFieldCode)
field_code = att_def_field.getFieldCode(flag)
objcode = f"%<\\_ObjId {block_ref.objectId().asOldId()}>%".format()
field_code = field_code.replace("?BlockRefId", objcode)
att_ref_field = Db.Field(field_code)
# att_ref_field.evaluate()
field_id = att_ref.setField(att_ref_field)
Db.Core.evaluateFields([field_id], Db.FieldEvalContext.kDemand)
else:
att_ref.setTextString(att_def.textString())
return block_ref |
Beta Was this translation helpful? Give feedback.
-
Just a note, Ax space works great for ZwCAD. BricsCAD has issues def doInsert(name: str):
axap = Ax.AcadApplication()
acdoc = axap.activeDocument()
axSpace = acdoc.modelSpace()
ref = axSpace.insertBlock(Ge.Point3d(2727, 1300, 0), name, Ge.Scale3d(1), 0)
return ref.objectId()
@Ap.Command()
def doit():
try:
ps, id, _ = Ed.Editor.entSel("\nPick a Block: ", Db.BlockReference.desc())
if ps != Ed.PromptStatus.eOk:
raise RuntimeError("Selection Error! {}: ".format(ps))
ref = Db.BlockReference(id)
newid = doInsert(ref.getBlockName())
newref = Db.BlockReference(newid)
atts = [Db.AttributeReference(_id) for _id in newref.attributeIds()]
for att in atts:
print(att.hasFields())
except Exception as err:
traceback.print_exception(err) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Using this example, I want to insert a reference to a block that contains attributes with fields relating to the state of that block reference. I've added the following to the code:
But the attribute isn't displaying correctly; I get something like this:
Beta Was this translation helpful? Give feedback.
All reactions