TheSwamp

Code Red => VB(A) => Topic started by: David Hall on January 29, 2008, 10:02:40 AM

Title: Math - 3D Solid Challenge
Post by: David Hall on January 29, 2008, 10:02:40 AM
Anybody up for a 3D math challenge?  What I need to do is locate 3 blocks in 3D Space, and Connect them with 2 pieces of 3d "pipe"  Should be easy enough with the math, its the connecting Im having problems with.
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on January 29, 2008, 10:06:39 AM
The blocks needed are here as well
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on January 29, 2008, 10:08:19 AM
Now here is the challenge, Write a routine, preferable in VBA to accommodate varying C.L. to C.L. (centerline to centerline) heights.  the picture shown is a 6 foot separation
Title: Re: Math - 3D Solid Challenge
Post by: MickD on January 29, 2008, 03:07:22 PM
Do the angles of the 'T' pieces remain the same? this means the centre of the 2 bottom pieces will vary then yes?

If this is the case you need to create 2 vectors from the pipes, scale them to suit the changed height between the centres.
I'll knock up a quick piccy when I get to work and make sure we're on the same page.
Title: Re: Math - 3D Solid Challenge
Post by: Bryco on January 29, 2008, 03:56:23 PM
The hard part is the inverse matrix in vba for extruding the pipes.
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on January 29, 2008, 03:59:09 PM
The angle is always 30degrees b/t the 2 down pipes, the 2 bottom pieces would then have to move to accomodate the height difference.  Also, the blocks I posted can be edited if we need to move the insertion points.  After playing with it today, I ended p moving the ins pt to be more inline with the down pipe.  The problem I ran into was the V-part at the top.  The center of the V-pipes does not go through the center of the top piece, so I am having trouble figuring out how to calc where to go.  Maybe Im just over looking something  simple.
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on January 29, 2008, 04:01:22 PM
Bryco, I was thinking of using hard-coded numbers from the ins pt of the top piece to get to the location of the pipe start, and then doing the TAN calc based on CL to CL distance to do the extrusion.  Or Im totally in left field
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on January 29, 2008, 04:02:10 PM
99.999999% of the time the UCS will be world, and that .0000001% of the time, It wont work anyway.
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on January 29, 2008, 04:03:26 PM
I was thinking of drawing lines from start to end point for the pipes, and extruding a circle w/ the normal set to the line, the length calced.  Similiar to the horizontal and vertical extrusions you helped me with about a year ago
Title: Re: Math - 3D Solid Challenge
Post by: MickD on January 29, 2008, 04:05:10 PM
Here's the math part anyway (I think :) )
Title: Re: Math - 3D Solid Challenge
Post by: MickD on January 29, 2008, 04:10:05 PM
IRT pipe extrusions, draw the circle/s, translate to point and set normal to line direction as Duh said and extrude the length of the line. Some calc's would also be need to located the 'actual' sp and ep of the pipe as it won't be the full length of the line.

To derive the cot and csc use these -

csc = 1/sinA
cot = 1/tanA

plug 'em in and away you go ;)
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on January 29, 2008, 04:11:33 PM
Mick, here is a pic showing the lower pieces moving inward to accomodate the shorter distance.
Title: Re: Math - 3D Solid Challenge
Post by: MickD on January 29, 2008, 04:18:04 PM
Good, that makes it easy then.
The length of the line to pnt1 in my piccy = 1/sinA x H
pnt1.x = -1/tanA x H
pnt1.y = -H


IRT to anything other than wcs, always do the modeling in wcs then xform into place, it's a lot easier.
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on January 29, 2008, 04:50:10 PM
IRT ?
Title: Re: Math - 3D Solid Challenge
Post by: MickD on January 29, 2008, 04:51:35 PM
In Regards To :)
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on January 29, 2008, 04:53:37 PM
Thats what I thought, but google didn't even know
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on January 29, 2008, 04:53:58 PM
what about the fact that the centerlines are off of the center of the upper part?
Title: Re: Math - 3D Solid Challenge
Post by: Cathy on January 29, 2008, 05:04:44 PM
Would putting some workpoints in your blocks help with some of this?  Like a point where the CLs cross, points where the pipes connect?

IRT -- odd I always use WRT (with respect to).  Is that just a difference is Aussie English and American English?  Or is just a difference in disciplines? 
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on January 29, 2008, 05:08:15 PM
I was thinking of calcing the workpoint in code, and then moving down and to the left/right from there to start the line.  If you look at the first pic, you can see some cross's inside the pipe, that is the start/stop point for the vertical anlge of pipe
Title: Re: Math - 3D Solid Challenge
Post by: MickD on January 29, 2008, 05:15:56 PM
>>what about the fact that the centerlines are off of the center of the upper part?

The angle is still the same so I'd translate the pipe 11/16 to the left and the bottom T the amount of the offset from insert point to the left, repeat on right side.

I'd have a go at this but I'm busy with something else at the moment but here's the steps I would take using the math I posted earlier -

Calc the pipe lengths and pnt1 on the left side
Insert the top pipe at origin
Insert the left bottom T at pnt1 and translate it the offset amount to the left
Extrude the pipe, translate it to the origin and the angle then translate it left the top T offset amount
(and along the line a bit if needed to clear cross pipe as it's shorter than the actual line length)
Copy and mirror over top T c.l. for right side.

If I get time later I'll code something up.
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on January 29, 2008, 05:22:00 PM
thanks
Title: Re: Math - 3D Solid Challenge
Post by: Bryco on January 30, 2008, 10:55:23 AM
I had some success with below code.
To the apex bock I added 2 regions on defpoints that are positiioned where you have the lines.
These regions are copied from the block itself then tranlated to the blockrefs position.
Then extruded.

Code: [Select]
Sub Cmd()

    Dim Ent As AcadEntity
    Dim B As AcadBlock
    Dim Br As AcadBlockReference
    Dim P, m, Ret
    Dim Ms As AcadBlock
    Dim Obj(0) As Object
    Dim C1 As AcadCircle, C2 As AcadCircle
    Dim R1 As AcadRegion
    Dim sBlock As String
    Dim Dist As Double, Ht As Double
    Dim Util As AcadUtility
   
    Set Util = ThisDrawing.Utility
    Set Ms = ThisDrawing.ModelSpace
    sBlock = "5020760"
    Util.GetEntity Ent, P, "Pick the 5020"
    If Not TypeOf Ent Is AcadBlockReference Then Exit Sub
    If Not Ent.Name = sBlock Then Exit Sub
    Set Br = Ent
    Set B = ThisDrawing.Blocks(sBlock)
    For Each Ent In B
        If TypeOf Ent Is AcadRegion Then
        Set Obj(0) = Ent
            Ret = ThisDrawing.CopyObjects(Obj, Ms)
            Set R1 = Ret(0)
            R1.Layer = "0"
            R1.Color = 2
            Exit For
        End If
    Next Ent
    Dist = Util.GetDistance(, "Centerline distance")
    If Dist < 17.0855 Then
        MsgBox ("Distance must be larger than 17")
        Exit Sub
    End If
    Ht = (Dist - 17.0855) / Cos(15 / 180 * PI) + 4
    m = BlockRefMatrix(Br)
    m = InverseMatrix(m)

    R1.TransformBy (m)
    Dim Tube As Acad3DSolid
    Set Tube = Ms.AddExtrudedSolid(R1, Ht, 0)
   
End Sub



Function BlockRefMatrix(oBref As AcadBlockReference) As Variant

    Dim V, m(3, 3) As Double, M1, M2
    Dim j As Integer
    Dim X, Y, Z, ins
    Dim Rot As Double
   
    Rot = oBref.Rotation
    Z = oBref.Normal
    V = GetOcsFromNormal(Z)
    X = V(0): Y = V(1)
    PrintP X
    PrintP Y
    ins = oBref.InsertionPoint
   
    M1 = RotZ(-Rot)
    For j = 0 To 2
        m(0, j) = X(j)
        m(1, j) = Y(j)
        m(2, j) = Z(j)
    Next j
    m(0, 3) = -ins(0): m(1, 3) = -ins(1): m(2, 3) = -ins(2)
    m(3, 3) = 1
    'PrintM M
    If Rot = 0 Then
        BlockRefMatrix = m
    Else
        M2 = M4xM4(m, M1)
        PrintM M2
        BlockRefMatrix = M2
    End If
   
   
   
End Function
Title: Re: Math - 3D Solid Challenge
Post by: MickD on January 30, 2008, 05:44:32 PM
I had some success with below code.
To the apex bock I added 2 regions on defpoints that are positiioned where you have the lines.
These regions are copied from the block itself then tranlated to the blockrefs position.
Then extruded.

Great idea Bryco! saves quite a bit of matrix mojo. Perhaps you can put the other bottom blocks in as well as nested in the correct alignment then just translate it along the line vector the given amount after calc'ing with the new height?
Title: Re: Math - 3D Solid Challenge
Post by: Bryco on January 30, 2008, 08:07:38 PM
I haven't done much coding for the creation and editing of 3d objects so I tried a circle first, only to find a cylinder doesn't follow a path.
So yes the region saves a bit of matricising (Pres. Bush speak).
After an hour I thought I would have the hard part whooped but alas the correct matrix is eluding me for the funky blocks. (Dang it). Works for the blocks rotated in the same plane.
Mick, I think straight vector math will establish the insertion point of the other blocks and when that is translated using the same magical matrix David can say "Bob's me uncle"
Title: Re: Math - 3D Solid Challenge
Post by: MickD on January 30, 2008, 08:12:53 PM
Do you mean to rotate the bottom T or to mirror it?

To create a mirror matrix you should be able to just negate some the vectors for axes and translation and transformBy.
If you are mirroring over the y axis, negate the x vector for example.

if it's the rotation of the blocks perhaps setting them up in the block drg in the correct location/orientation for simple insert (create a block for left and right say).
Title: Re: Math - 3D Solid Challenge
Post by: MickD on January 30, 2008, 08:36:21 PM
It's even easier, I just noticed that ent's have a Mirror3d method. that may help.
Title: Re: Math - 3D Solid Challenge
Post by: MickD on January 30, 2008, 09:52:48 PM
This is what I'm thinking -
Title: Re: Math - 3D Solid Challenge
Post by: Bryco on January 31, 2008, 12:27:43 AM
This works, finally. David the last part is mirror3d as Mick suggests.
Transpose rather than inverse threw for a while as usual (More brains reqd.)
There is a weird variety of math used here, but they all relate to normals somewhere.
Code: [Select]
Option Explicit
Const PI As Double = 3.14159265358979


Sub Dav()

    Dim Ent As AcadEntity
    Dim B As AcadBlock
    Dim Br As AcadBlockReference
    Dim P, M, M1, Ret
    Dim Ms As AcadBlock
    Dim Obj(0) As Object
    Dim C1 As AcadCircle, C2 As AcadCircle
    Dim R1 As AcadRegion
    Dim sBlock As String
    Dim Dist As Double, Ht As Double
    Dim Util As AcadUtility
    Dim Zero(2) As Double
   
   
    Set Util = ThisDrawing.Utility
    Set Ms = ThisDrawing.ModelSpace
    sBlock = "5020760"
    Util.GetEntity Ent, P, "Pick the 5020"
    If Not TypeOf Ent Is AcadBlockReference Then Exit Sub
    If Not Ent.Name = sBlock Then Exit Sub
    Set Br = Ent
    Set B = ThisDrawing.Blocks(sBlock)
    For Each Ent In B
        If TypeOf Ent Is AcadRegion Then
        Set Obj(0) = Ent
            Ret = ThisDrawing.CopyObjects(Obj, Ms)
            Set R1 = Ret(0)
            R1.Layer = "0"
            R1.color = 2
            Exit For
        End If
    Next Ent
    Dist = Util.GetDistance(, "Centerline distance")
    If Dist < 17.0855 Then
        MsgBox ("Distance must be larger than 17")
        Exit Sub
    End If
    Ht = (Dist - 17.0855) / Cos(15 / 180 * PI) + 4
    M = BlockRefMatrix(Br)

    M1 = Transpose(M)
    R1.TransformBy (M1)
    Dim Tube As Acad3DSolid
    Set Tube = Ms.AddExtrudedSolid(R1, Ht, 0)
    R1.Delete
   
    Dim width As Double
    Dim inspt(2) As Double
    Dim Br2 As AcadBlockReference
   
    width = (Tan(15 / 180 * PI) * (Dist - 17.0855)) + 4.0682
    inspt(1) = -width: inspt(2) = -Dist
   

    P = TransformPt2(M1, inspt)
    Set Br2 = Ms.InsertBlock(Zero, "5141714", 1, 1, 1, 0)
    Br2.Normal = Br.Normal
    Br2.Rotation = Br.Rotation
    Br2.Move Zero, P
   
End Sub









Function BlockRefMatrix(oBref As AcadBlockReference) As Variant

    Dim V, M(3, 3) As Double, M1, M2
    Dim j As Integer
    Dim X, Y, Z, ins
    Dim Rot As Double
   
    Rot = oBref.Rotation
    Z = oBref.Normal
    V = GetOcsFromNormal(Z)
    X = V(0): Y = V(1)

    ins = oBref.InsertionPoint
   
    M1 = RotZ(-Rot)
    For j = 0 To 2
        M(0, j) = X(j)
        M(1, j) = Y(j)
        M(2, j) = Z(j)
        M(3, j) = ins(j)
    Next j
    M(3, 3) = 1

    If Rot = 0 Then
        BlockRefMatrix = M
    Else
        M2 = M4xM4(M, M1)
        BlockRefMatrix = M2
    End If
   
   
End Function






Function Transpose(Matrix As Variant) As Variant

    Dim iCnt As Integer, jCnt As Integer
    Dim transMat(0 To 3, 0 To 3) As Double
    Dim I As Integer, j As Integer
    iCnt = UBound(Matrix, 1)
    jCnt = UBound(Matrix, 2)
    For I = 0 To iCnt
        For j = 0 To jCnt
            transMat(I, j) = Matrix(j, I)
        Next j
    Next I
    Transpose = transMat
   
End Function



Function TransformPt2(M As Variant, P1 As Variant) As Variant
    Dim I As Integer
    Dim X As Double, Y As Double, Z As Double, d As Double
    Dim P(3) As Double
    Dim P2(2) As Double
    For I = 0 To 2
        P(I) = P1(I)
    Next
    P(3) = 1
   
    For I = 0 To 3
        X = X + P(I) * M(0, I)
        Y = Y + P(I) * M(1, I)
        Z = Z + P(I) * M(2, I)
        d = d + P(I) * M(3, I)
    Next
    P2(0) = X: P2(1) = Y: P2(2) = Z
    TransformPt2 = P2

End Function



Function GetOcsFromNormal(N As Variant) As Variant
    'Arbitrary Axis Algorithm in dxf help
    'N is the normal vector.
    'Wy is the world Y axis, which is always (0,1,0).
    'Wz is the world Z axis, which is always (0,0,1).
    Dim Wy(2) As Double
    Dim Wz(2) As Double
    Dim Nx As Double, Ny As Double
    Dim Ax, Ay, Ocs(1) As Variant
   
    N = NormaliseVector(N)
    Wy(0) = 0: Wy(1) = 1: Wy(2) = 0
    Wz(0) = 0: Wz(1) = 0: Wz(2) = 1
    Nx = N(0): Ny = N(1)
    If (Abs(Nx) < 1 / 64) And (Abs(Ny) < 1 / 64) Then
         'Ax = Wy X N (where “X” is the cross-product operator).
         Ax = Crossproduct(Wy, N)
    Else
         Ax = Crossproduct(Wz, N)
    End If
    Ocs(0) = Ax
 
    Ay = Crossproduct(N, Ax)
    Ocs(1) = Ay
    GetOcsFromNormal = Ocs

End Function



Function RotZ(Ang As Double) As Variant
   'Rotate by an angle around the z axis
    Dim M
    Dim CosAng As Double, sinAng As Double
   
    CosAng = Cos(Ang): sinAng = Sin(Ang)
    M = IDMatrix
    M(0, 0) = CosAng
    M(0, 1) = -sinAng
    M(1, 0) = sinAng
    M(1, 1) = CosAng
     
    RotZ = M

End Function


Function M4xM4(M1, M2) As Variant
    'Matrix x matrix
    Dim M(3, 3) As Double
    Dim I As Integer, j As Integer
    Dim k As Integer
    Dim Sum As Double
   
    For I = 0 To 3
        For j = 0 To 3
            For k = 0 To 3
                Sum = Sum + M1(k, j) * M2(I, k)
            Next k
            M(I, j) = Sum
            Sum = 0
        Next j
    Next I
    M4xM4 = M
   
End Function


Function NormaliseVector(V As Variant) As Variant
    Dim Unit As Double
    Dim Vn(2) As Double
    Unit = Sqr(V(0) * V(0) + V(1) * V(1) + V(2) * V(2))
    Vn(0) = V(0) / Unit: Vn(1) = V(1) / Unit: Vn(2) = V(2) / Unit
    NormaliseVector = Vn
End Function


Function Crossproduct(A, B) As Variant

    Dim Ax As Double, Ay As Double, Az As Double
    Dim Bx As Double, By As Double, Bz As Double
    Dim Unit As Double
    Dim c(2) As Double
    'get CrossProduct
    Ax = A(0): Ay = A(1): Az = A(2)
    Bx = B(0): By = B(1): Bz = B(2)
   
    c(0) = Ay * Bz - Az * By
    c(1) = Az * Bx - Ax * Bz
    c(2) = Ax * By - Ay * Bx
   
    'Convert to unit normal
    Unit = Sqr(c(0) * c(0) + c(1) * c(1) + c(2) * c(2))
    c(0) = c(0) / Unit: c(1) = c(1) / Unit: c(2) = c(2) / Unit
    Crossproduct = c

End Function



Function IDMatrix()
    Dim M(3, 3) As Double
    Dim I As Integer, j As Integer
    M(0, 0) = 1
    M(1, 1) = 1
    M(2, 2) = 1
    M(3, 3) = 1
    IDMatrix = M
   
End Function
Title: Re: Math - 3D Solid Challenge
Post by: Bryco on January 31, 2008, 12:39:12 AM
Test dwg.
Title: Re: Math - 3D Solid Challenge
Post by: SEANT on January 31, 2008, 07:02:10 AM
Excellent work once again gentlemen.  A word of caution; if you persist in this development of “one click design creation”, managers may do the work themselves and figure they need CAD techs no longer. :-)

Bryco, I see you’ve expanded the scope of the project to include “other’world’ly” alignments – certainly a welcome inclusion for those of us with chaotic structures.  The one recommendation I would make for dealing with these non-aligned setups, like the block in CmdrduhTest.dwg @ (99.9462, 265.5402, 0.4912), is to switch order of operation from:

Br2.Normal = Br.Normal
Br2.Rotation = Br.Rotation

To:

Br2.Rotation = Br.Rotation 
Br2.Normal = Br.Normal
   
Title: Re: Math - 3D Solid Challenge
Post by: Bryco on January 31, 2008, 10:44:54 AM
thanks Sean, re the rotation,normal is there a set of circumstances where that is necessary?
Title: Re: Math - 3D Solid Challenge
Post by: SEANT on January 31, 2008, 12:18:34 PM
I should mention that I'm still using 2004 and may be missing some code changes of the later AutoCAD releases. 

The example I refered to does not align exactly unless those lines are switched.  This example also cause my setup problems.
Title: Re: Math - 3D Solid Challenge
Post by: Bryco on January 31, 2008, 01:34:08 PM
Cool, it has no probs in 2008, in 2004 it needs to be written as you say.
There's a mystery.
Title: Re: Math - 3D Solid Challenge
Post by: SEANT on January 31, 2008, 06:38:19 PM
I suppose it is sensible to expect the VBA 3d interface to change with so much extra going on in 2007/2008.  Still, I’ve got to wonder if the boys at Autodesk just like to keep us guessing.  They certainly like to keep us upgrading.

Once again, nice job.
Title: Re: Math - 3D Solid Challenge
Post by: Bryco on February 01, 2008, 05:23:34 PM
CmdrDuh-did it do what you wanted?
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on February 01, 2008, 06:48:07 PM
I have been gone for 2 days getting my eyes tested.  So I haven't had a chance to try it yet.  Monday I intend to try it.  I'll definetly let you know!!!


Thanks
Title: Re: Math - 3D Solid Challenge
Post by: Bryco on February 18, 2008, 08:42:02 PM
Well while it's monday, it is the third monday that has passed.
A bit rude mate.
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on February 20, 2008, 12:20:32 PM
Sorry Bryco, I haven't had time to do any programming at work lately.  This week Im home with sick kids, and I have the flu.
Title: Re: Math - 3D Solid Challenge
Post by: Chuck Gabriel on February 20, 2008, 05:13:11 PM
Sorry Bryco, I haven't had time to do any programming at work lately.  This week Im home with sick kids, and I have the flu.

Get well soon (all of you).
Title: Re: Math - 3D Solid Challenge
Post by: Kerry on February 20, 2008, 06:49:15 PM

Rum and limes David  .. rum for you, limes for the kids :-)
Title: Re: Math - 3D Solid Challenge
Post by: Bryco on February 21, 2008, 04:06:32 PM
Get well all.
Title: Re: Math - 3D Solid Challenge
Post by: Maverick® on February 21, 2008, 04:50:43 PM

Rum and limes David

Irie Mon   8-) :-)

Title: Re: Math - 3D Solid Challenge
Post by: David Hall on February 25, 2008, 09:15:09 AM
Well I sent the kids back to school today, and Im back at work, so lets see how that bit of code worked.  :-)
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on February 25, 2008, 09:15:37 AM

Rum and limes David  .. rum for you, limes for the kids :-)
Just what the Dr. ordered  :-)
Title: Re: Math - 3D Solid Challenge
Post by: David Hall on February 25, 2008, 10:38:07 AM
That is awesome!  I just need to work out the mirror part, but that was way cool.  Bryco, I am definetly going to have to study the matrix stuff more, because that was hard core
Title: Re: Math - 3D Solid Challenge
Post by: MickD on February 25, 2008, 03:49:56 PM
>> I am definitely going to have to study the matrix stuff

Yep, once you understand vectors and matrices (which are just groups/lists of vectors) you can do pretty much anything you like in 3 dimensions ;)
Title: Re: Math - 3D Solid Challenge
Post by: Bryco on February 25, 2008, 07:58:09 PM
Glad you like it, David.
C# has all that right at your fingertips but I wonder how difficult it would be to use them without understanding them.
I just wrote a mirror jig for C# and almost laughed when I figured out the jig needed was a rotation jig.
You mirror it once then rotate it where you want (kinda sorta)