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

set block POsition Z to 0

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
imagination_s
2168 Views, 10 Replies

set block POsition Z to 0

Hello Everyone

How can i change or set  the Position Z of a block in autocad  to 0  by using .net  vb or c#

 

please help

1.png

Thankyou

10 REPLIES 10
Message 2 of 11
hgasty1001
in reply to: imagination_s

Hi,

 

See the documentation (or google)  on Entity.TransformBy method

 

Gaston Nunez

Message 3 of 11
imagination_s
in reply to: hgasty1001

Thankyou

I will have a look

Message 4 of 11
imagination_s
in reply to: hgasty1001

So far i want to set every entity Position Z to 0

So my code looks like this

 

<CommandMethod("changeme", CommandFlags.UsePickSet Or CommandFlags.Redraw)> _
        Public Shared Sub testchange()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Dim rb As New ResultBuffer()
            Try

                rb.Add(New TypedValue(5005, "_Zoom"))
                rb.Add(New TypedValue(5005, "_Extents"))
                acedCmd(rb.UnmanagedObject)

                Dim ucs As Matrix3d = ed.CurrentUserCoordinateSystem
                Dim ccos As CoordinateSystem3d = ucs.CoordinateSystem3d
                Dim orig As Point3d = ccos.Origin.TransformBy(Matrix3d.Identity)
                ' select all objects
                Dim sset As SelectionSet = ed.SelectAll().Value
                If sset Is Nothing Then
                    Return
                End If

                Dim mmx As New Matrix3d()
                Using tr As Transaction = doc.TransactionManager.StartTransaction()
                    ' iterate through selected objects
                    For Each id As ObjectId In sset.GetObjectIds()

                        Dim ent As Entity = DirectCast(tr.GetObject(id, OpenMode.ForWrite), Entity)

                        Dim ext As Extents3d = ent.GeometricExtents
                        Dim mp As Point3d = New Point3d(orig.X, orig.Y, 0).TransformBy(Matrix3d.Identity)
                        mmx = Matrix3d.Displacement(orig - mp)
                        'If ext = 0 Then
                        ' Dim minpt As Point3d = ext.TransformBy(Matrix3d.Identity)
                        ' collect entities to List for the future work
                        ent.TransformBy(mmx)
                        ent.UpgradeOpen()

                        'End If
                    Next

                    tr.Commit()
                End Using
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                ed.WriteMessage((vbLf + ex.Message & vbLf) + ex.StackTrace)
            Finally
                rb = New ResultBuffer()
                rb.Add(New TypedValue(5005, "_Zoom"))
                rb.Add(New TypedValue(5005, "_Extents"))
                acedCmd(rb.UnmanagedObject)
            End Try
        End Sub

 

Ok.So i explain it

First i want to get for each entity position origin  X,Y,Z

next a create a new set of point based on originx,origin.y  and set the Z to 0

now change the entity to my new points 3d

get transformby and upgrade open

So please tell me what i am doing wrong ?

 

Message 5 of 11
hgasty1001
in reply to: imagination_s

Hi,

 

I think you have to use this work flow:

 

1.- Get your selection set

2.-Iterate the SS inside a transaction

3.-Get the position of the current entitie (say cEnt) in a Point3d variable (say BasePoint)

4.-Obtain a new Matrix3D displacement using:

 

Dim m3d as Matrix3d= new Matrix3D.Displacement(BasePoint.GetVectorTo(New Point3D(BasePoint.X,BasePoint.Y,0).TransformBy(UCS))

 

5.-Transform the current entitie using: cEnt.TransformBy(m3d)

6.-Commit the transaction

 

Gaston Nunez

 

 

Message 6 of 11
imagination_s
in reply to: hgasty1001

Thankyou very much for your help 🙂

I will try to put it all togheter

Message 7 of 11
imagination_s
in reply to: hgasty1001

Please tell me how do i

Get the position of the current entitie (say cEnt) in a Point3d variable (say BasePoint)

Message 8 of 11
hgasty1001
in reply to: imagination_s

Hi,

 

If the entity it's a DBPoint, blockreference, mtext,text, table, and some others, they have a position property. I'm not sure about curve objects, maybe projecting them to the plane Z=0 should work, or just taking some point on the curve object (for circles the centerpoint property). I'm assuming that the objects are in different z position, if they (all) are in the same z, you can calculate the geometric extent of the selection set and move each entity using the min or max of the extents to obtain the transformation matrix.

 

Gaston Nunez

Message 9 of 11
imagination_s
in reply to: hgasty1001

Hello,

Like you said ,it's a point so it must be DBPoint.

So a need to figure out how to get X<Y<Z coodonates of the Point

Message 10 of 11
hgasty1001
in reply to: imagination_s

Hi,

 

This is a copy from the SDK docs about DBPoint:

 

This .NET class wraps the AcDbPoint ObjectARX class. 

It represents the point entity within AutoCAD. A point entity has a position value to locate it in space. It also has a normal vector, which is used to determine its extrusion direction and to determine the plane on which to display the point's graphics if they are something other than a "." (the PDMODE system variable controls this display). The point entity has a thickness that is its "length" in the direction of its normal vector (that is, its extrusion length).

 

Gaston Nunez

Message 11 of 11
imagination_s
in reply to: hgasty1001

Thankyou for your help 🙂

I figure it out how to get position Z from blok reference  and rewrite it to 0

Here is my function

 

   Public Sub Changeme()
        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database
        acDoc.LockDocument()
        Dim values() As TypedValue = { _
    New TypedValue(DxfCode.BlockName, "DOT")
    }
        Dim sfilter As New SelectionFilter(values)

        Dim SelOpts As New PromptSelectionOptions()
        SelOpts.MessageForAdding = "Select Block:"
        SelOpts.AllowDuplicates = True
        'Make the selection:
        Dim res As PromptSelectionResult = ed.GetSelection(SelOpts, sfilter)

        If Not res.Status = PromptStatus.OK Then Return

        Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
        Dim idarray As ObjectId() = SS.GetObjectIds()
        Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
        Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
        Dim myT As Transaction = tm.StartTransaction()
        Dim dwg As Document = CadApp.DocumentManager.MdiActiveDocument
      
        Try


            'Dim id As ObjectId
            Dim ucs As Matrix3d = ed.CurrentUserCoordinateSystem
            Dim cEnt As Entity
            ' For Each id In Entity
            For Each id In idarray

                cEnt = tm.GetObject(id, OpenMode.ForRead, False)
                Dim bref As BlockReference = DirectCast(tm.GetObject(id, OpenMode.ForRead), BlockReference)
                Dim x As Double
                Dim y As Double
                Dim z As Double
                x = bref.Position.X
                y = bref.Position.Y
                z = -bref.Position.Z
           
                Dim tMoveVec As Geometry.Vector3d = New Geometry.Vector3d(0, 0, z)
                Dim m3d As Matrix3d
                m3d = New Matrix3d
                Dim tTransMat As Geometry.Matrix3d = Geometry.Matrix3d.Displacement(tMoveVec)
             
                cEnt = tm.GetObject(id, OpenMode.ForWrite, True)
                cEnt.Layer = "Converted"
                cEnt.ColorIndex = 0
                cEnt.UpgradeOpen()
                ' apply transformation matrix
                cEnt.TransformBy(tTransMat)

            Next

            myT.Commit()

        Finally
            myT.Dispose()
        End Try
    End Sub

 

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost