Custom grip moves but dynamic block reference doesn't

Custom grip moves but dynamic block reference doesn't

OrmArTHe
Enthusiast Enthusiast
768 Views
6 Replies
Message 1 of 7

Custom grip moves but dynamic block reference doesn't

OrmArTHe
Enthusiast
Enthusiast

I'm inserting a dynamic block reference into drawing that has a custom grip used for moving the block.
By changing the value of DynamicBlockReferenceProperty position property, the grip is at the correct position after insert but the block geometry stays in place.

Any ideas how to fix this issue?

Code (this snippet is inside a transaction):

 

            var blkRef = new BlockReference(Point3d.Origin, blkRecId);
            var currentSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
            var blkRefId = currentSpace.AppendEntity(blkRef);
            tr.AddNewlyCreatedDBObject(blkRef, true);
            if (blkRef.IsDynamicBlock)
            {
                var dynProps = blkRef.DynamicBlockReferencePropertyCollection;
                foreach (DynamicBlockReferenceProperty prop in dynProps)
                {
                    if (prop.PropertyName == "Position1 X")
                        prop.Value = 1.0;
                    if (prop.PropertyName == "Position1 Y")
                        prop.Value = 1.0;
                }
            }

 

0 Likes
Accepted solutions (1)
769 Views
6 Replies
Replies (6)
Message 2 of 7

_gile
Consultant
Consultant

Hi,

The issue seems to be related to the block itself. You should attach it.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 7

OrmArTHe
Enthusiast
Enthusiast

Here it is...

0 Likes
Message 4 of 7

norman.yuan
Mentor
Mentor

How do you do the "custom grip"? GripOverrule? If so, how is the Overrule's filter is set? I assume the Overrule targets BlockReference. Do you filter BlockReferences by BlockReference.Name? 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 7

OrmArTHe
Enthusiast
Enthusiast

I've tried to use GetGripPoints and MoveGripPointsAt methods and now neither the grip nor the block geometry move...

            if (blkRef.IsDynamicBlock)
            {
                double position1X = 0.0;
                double position1Y = 0.0;
                var dynProps = blkRef.DynamicBlockReferencePropertyCollection;
                foreach (DynamicBlockReferenceProperty prop in dynProps)
                {
                    if (prop.PropertyName == "Position1 X")
                    {
                        position1X = (double)prop.Value;
                    }
                    if (prop.PropertyName == "Position1 Y")
                    {
                        position1Y = (double)prop.Value;
                    }
                }
                // get grip points
                var gripPoints = new Point3dCollection();
                var gripPosition = new Point3d();
                gripPosition = new Point3d(gripPosition.X + position1X, gripPosition.Y + position1Y, gripPosition.Z);
                gripPoints.Add(gripPosition);
                var snapModes = new IntegerCollection();
                var geometryids = new IntegerCollection();
                blkRef.GetGripPoints(gripPoints, snapModes, geometryids);
                // move grip points
                var indices = new IntegerCollection();
                indices.Add(0);
                var offset = new Vector3d(2.0, 2.0, 0.0);
                blkRef.MoveGripPointsAt(indices, offset);
            }




0 Likes
Message 6 of 7

_gile
Consultant
Consultant
Accepted solution

The "geometry" of your block is only attribute references.

When inserting an attributed block, you have to explicitly, create the attribute references using the attribute definitions in the the block definition as templates and add them to the block reference attribute collection.

var blkRef = new BlockReference(Point3d.Origin, blkRecId);
var currentSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
var blkRefId = currentSpace.AppendEntity(blkRef);
tr.AddNewlyCreatedDBObject(blkRef, true);

// Add attribte references
var blkRec = (BlockTableRecord)tr.GetObject(blkRecId, OpenMode.ForRead);
foreach (ObjectId id in blkRec)
{
    if (id.ObjectClass.Name == "AcDbAttributeDefinition")
    {
        var attDef = (AttributeDefinition)tr.GetObject(id, OpenMode.ForRead);
        if (!attDef.Constant)
        {
            var attRef = new AttributeReference();
            attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);
            blkRef.AttributeCollection.AppendAttribute(attRef);
            tr.AddNewlyCreatedDBObject(attRef, true);
        }
    }
}

if (blkRef.IsDynamicBlock)
{
    var dynProps = blkRef.DynamicBlockReferencePropertyCollection;
    foreach (DynamicBlockReferenceProperty prop in dynProps)
    {
        if (prop.PropertyName == "Position1 X")
            prop.Value = 1.0;
        if (prop.PropertyName == "Position1 Y")
            prop.Value = 1.0;
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 7

OrmArTHe
Enthusiast
Enthusiast

Actually, I have that code block related to adding attribute references. The issue was that the code for accessing and changing the grip point position was above it, instead of below it.
Once again thank you, @_gile !

0 Likes