TurboCAD Forums

The Ultimate Resource for TurboCAD Knowledge

Register
 
Remember to do a search before you ask a question!!!

Excel to TCAD
Read 5476 times
* March 30, 2011, 01:19:07 PM
It was recently pointed out to me that my earlier contribution http://forums.turbocad.com/index.php/topic,1633.0.html showed how to go from TCad to Excel but not from Excel to TCAD

To go from Excel to TCad I make a lot of use of the Excel Cells function.

By way of example

Code: [Select]
Dim wksWKS as Excel.Worksheet
Dim RowX0 as Long
Dim ColumnX0 as Integer
Dim RowY0 as Long
Dim ColumnY0 as Integer
Dim RowX1 as Long
Dim ColumnX1 as Integer
Dim RowY1 as Long
Dim ColumnY1 as Integer
Dim X0 as Double
Dim Y0 as Double
Dim X1 as Double
Dim Y1 as Double

Dim drDr As IMSIGX.Drawing

Dim grGr As IMSIGX.Graphic
Dim grsGrs As IMSIGX.Graphics
set wksWKS = Excel.Application.ActiveSheet
X0  = wksWKS.Cells(RowX0, ColumnX0)
Y0  = wksWKS.Cells(RowY0, ColumnY0)
X1  = wksWKS.Cells(RowX1, ColumnX1)
Y1  = wksWKS.Cells(RowY1, ColumnY1)
Set drDr = IMSIGX.ActiveDrawing
Set grsGrs = IMSIGX.ActiveDrawing.Graphics

'and heres the part thats hard to figure

Set grGr = grsGrs.AddLineSingle(X0, Y0, 0, X1, Y1, 0)
'Set grGr = grsGrs.AddLineSingle(X0, Y0, Z0, X1, Y1, Z1)


The above code was off the top of my head and was NOT checked for errors.

After reviewing the above code I think the  active drawing object drDr is unnecessary; alternatively,
Set grsGrs = IMSIGX.ActiveDrawing.Graphics could be rewritten as Set grsGrs = drDR.Graphics, and the drDr object retained.

I find the TurboCad SDK help file tcsdk.chm is of great help in sorting out the various TurboCad objects, properties and methods.

I try to use a semblance of hungarian notation with my TCad objects; consequently, Drawing objects have a "dr" prefix, a Graphic object has a "gr" prefix, a Graphics object has a "grs" prefix, an Application object has an "app" prefix  and so on.

I hope it helps.

CWC
« Last Edit: March 30, 2011, 03:08:28 PM by cwcookman »

Logged


* April 14, 2011, 02:36:59 AM
#1
Thanks CWCookman.

The code as provided work but not when the set grGR portion is in a loop. An Error code 424 - object required is issued and the program halts.

Description of code.
Spreadsheet has a number of points as a counter for control loops with the xyz points for the lines in subsequent rows and columns.

The following routine was based on the proposal.  When stepped program works until it gets to the loop before the end of the sub where the writting whould happen in TC.  The error code pops up and I am at a lost for a solution.

Whole routine:
Sub Line_From_Excel()
'Define general variables
    Dim Counter, Points As Integer          ' Counter is control and points are the amount of points to be used
    Dim Row, Column As Integer              ' Row and cloumn is reference for the data
    Dim X(), Y(), Z() As Double             ' XYZ coordinates of the point from where the lines need to be drawn
   
    Set wksWKS = Excel.Application.ActiveSheet ' Set the active sheet as the reference
   
    Points = wksWKS.Cells(1, 2).Value      'Read the amount of points that need to be converted into lines in TC
       
    ReDim X(Points), Y(Points), Z(Points)   ' Ensure the size of the arrays are correct
       
    For Counter = 1 To Points               ' Read the line points into the array variables
        X(Counter) = wksWKS.Cells(Counter + 1, 2).Value
        Y(Counter) = wksWKS.Cells(Counter + 1, 3).Value
        Z(Counter) = wksWKS.Cells(Counter + 1, 4).Value
    Next Counter

' Moving the info to TurboCad
' Setting up the system to know of TC

    Dim drDr As IMSIGX.Drawing
    Dim grGr As IMSIGX.Graphic
    Dim grsGrs As IMSIGX.Graphics

    Set drDr = IMSIGX.ActiveDrawing
    Set grsGrs = IMSIGX.ActiveDrawing.Graphics
   
    'and heres the part thats hard to figure - this is actually not the hard part, it is when you like to make it more useful that the hard part actually starts. The same code is implemented in the loop and then it doesnt work? That is hard.   
   
Set grGr = grsGrs.AddLineSingle(1, 1, 1, 3, 3, 3) ' This works   
    For Counter = 1 To Points - 1 ' Assuming that the lines have the same starting point as the previous end point there would be points -1 lines
        Set grGr = Grs.AddLineSingle(1, 1, 1, 3, 3, 3) ' This doesnt work, what on earth is the difference?
        'Set grGr = Grs.AddLineSingle(X(Counter), Y(Counter), Z(Counter), X(Counter + 1), Y(Counter + 1), Z(Counter + 1))
    Next Counter
   
End Sub

Logged


* April 14, 2011, 06:59:31 AM
#2
HI Jacobusdt,

i don´t see any definition of the variable "Grs".  Try "Option explicit"  to see if its somewhere
defined in the code


mike

Logged


* April 14, 2011, 12:28:15 PM
#3
Hello
I don´t know if I got the problem.
Here is a code that I found in my library. It works fine for me.

---------------------------------------------------
Private Sub draw_lines()
 
    Dim TC_app As IMSIGX.Application
    Dim Dr As Drawing
    Dim gr, Grs As Graphics
    Dim Vi, vvi As View
    Set TC_app = GetObject(, "TurboCAD.Application")
   
    TC_app.Visible = True
    TC_app.ActiveDrawing.Activate
    'Set Dr = TC_app.ActiveDrawing
    Set gr = TC_app.ActiveDrawing.Graphics
    'TC_app.Drawings.Open ("YourFileNameHere.TCW")
    Set Grs = gr.AddLineSingle(0, 0, 0, -50, -10, 0)
    TC_app.ActiveDrawing.ActiveView.Refresh
 For i = 0 To 5
     Set Grs = gr.AddLineSingle(0, 0, 0, -50 + i, -10 + i, 0)
    TC_app.ActiveDrawing.ActiveView.Refresh
Next

End Sub

-------------------------
running the macro in Excel-vba
Excel 2002, TC18

best regards
Bernd

Logged


* April 15, 2011, 11:55:25 AM
#4
Here is a more formalized version of my earlier post.

I have annotated the example.

My first example was off the top of my head and was not checked for errors, this time I error checked the code.

Enjoy,
CWC

Code: [Select]
'**********************************************************************************************************

'**********************************************************************************************************
' Subroutene......: DemonstrateXL2Tcad
' Description.....: Sets up Cells (Row and Column Values) to store Vertex Values
'                 : Calls the TCadLine Function to place a line into the Active TurboCad Drawing
'                 : Calls a message box to tell you whether the call to the TCadLine Function worked
'
' Requires........: TCad to be active with a blank drawing
'                 : The Active Sheet of Excel to be blank
'
' Arguments.......: None
'
' Returns.........: A Line is placed in the active drawing of TCad
'**********************************************************************************************************

'**********************************************************************************************************
Sub DemonstrateXL2Tcad()
    Dim wksWKS As Excel.Worksheet
    Dim boolDidItWork As Boolean
    Dim RowX1 As Long
    Dim ColumnX1 As Integer
    Dim RowY1 As Long
    Dim ColumnY1 As Integer
    Dim RowZ1 As Long
    Dim ColumnZ1 As Integer
    Dim RowX2 As Long
    Dim ColumnX2 As Integer
    Dim RowY2 As Long
    Dim ColumnY2 As Integer
    Dim RowZ2 As Long
    Dim ColumnZ2 As Integer
    Dim X1 As Double
    Dim Y1 As Double
    Dim Z1 As Double
    Dim X2 As Double
    Dim Y2 As Double
    Dim Z2 As Double
        Set wksWKS = Excel.Application.ActiveSheet
           
            'make up some values for the Excel Cell Coordinates (rows and columns)
            RowX1 = 1
            ColumnX1 = 1
            RowY1 = 1
            ColumnY1 = 2
            RowZ1 = 1
            ColumnZ1 = 3
            RowX2 = 2
            ColumnX2 = 1
            RowY2 = 2
            ColumnY2 = 2
            RowZ2 = 2
            ColumnZ2 = 3
           
            'make up some values for the Coordinates and place them in Excel
            wksWKS.Cells(RowX1, ColumnX1) = 10
            wksWKS.Cells(RowY1, ColumnY1) = 20
            wksWKS.Cells(RowZ1, ColumnZ1) = 0
            wksWKS.Cells(RowX2, ColumnX2) = 30
            wksWKS.Cells(RowY2, ColumnY2) = 40
            wksWKS.Cells(RowZ2, ColumnZ2) = 0
           
            'retreive the coordinates from excel
            X1 = wksWKS.Cells(RowX1, ColumnX1)
            Y1 = wksWKS.Cells(RowY1, ColumnY1)
            Z1 = wksWKS.Cells(RowZ1, ColumnZ1)
            X2 = wksWKS.Cells(RowX2, ColumnX2)
            Y2 = wksWKS.Cells(RowY2, ColumnY2)
            Z2 = wksWKS.Cells(RowZ2, ColumnZ2)
           
            'send the coordinates to the line Function
            boolDidItWork = TCadLine(X1, Y1, Z1, X2, Y2, Z2)
                If boolDidItWork = True Then
                    MsgBox "Yes, it worked"
                ElseIf boolDidItWork = False Then
                    MsgBox "No, it did not work"
                End If
       
        'Clean up by releasing all objects
        Set wksWKS = Nothing
End Sub 'DemonstrateXL2Tcad
'**********************************************************************************************************

'**********************************************************************************************************
' Subroutene......: TCadLine
' Description.....: Sets up Cells (Row and Column Values) to store Vertex Values
'                 : Calls the TCadLine Function to place a line into the Active TurboCad Drawing
'                 : Calls a message box to tell you whether the call to the TCadLine Function worked
'
' Requires........: TCad to be active with a blank drawing
'                 : The Active Sheet of Excel to be blank
'
' Arguments.......: X1 the "X" coordinate of the first vertex
'                 : Y1 the "Y" coordinate of the first vertex
'                 : Z1 the "Z" coordinate of the first vertex
'                 : X2 the "X" coordinate of the Second vertex
'                 : Y2 the "Y" coordinate of the Second vertex
'                 : Z2 the "Z" coordinate of the Second vertex
'
' Returns.........: True if the Line was placed in the active drawing of TCad
'                 : False if there was an error
'**********************************************************************************************************

'**********************************************************************************************************

Function TCadLine(X1 As Double, Y1 As Double, Z1 As Double, X2 As Double, Y2 As Double, Z2 As Double) As Boolean

Dim appIMSI As IMSIGX.Application
Dim drDr As IMSIGX.Drawing
Dim grGr As IMSIGX.Graphic
Dim grsGrs As IMSIGX.Graphics
On Error GoTo errTCadLine
TCadLine = False
Set appIMSI = GetObject(, "IMSIGX.Application") 'Get a reference to the TurboCad Application object
Set drDr = appIMSI.ActiveDrawing 'Get a reference to the TurboCad Active Drawing object
Set grsGrs = drDr.Graphics 'Get a reference to the Graphics Collection object of the Active drawingSet grGr = grsGrs.AddLineSingle(X0, Y0, 0, X1, Y1, 0) 'add a Graphic to the Graphics Collection of the Active drawing
Set grGr = grsGrs.AddLineSingle(X1, Y1, Z1, X2, Y2, Z2) 'Add a line (a Graphic) to the Graphic(s) collection
TCadLine = True
'Clean up by releasing all objects
errTCadLine:
Set grGr = Nothing
Set grsGrs = Nothing
Set drDr = Nothing
Set appIMSI = Nothing

End Function 'TCadLine


Logged


* April 15, 2011, 01:22:43 PM
#5
I agree with Mikek.  I always use "Option Explicit" at the top of my Cdoe.  "Option explicit" forces us to declare (Dim) every variable. Undeclared variables (and mis-spelled variables) will throw an error.  I agree with Mikek that Jacobusdts' problem is a spelling error.  See the bottom of this post.

In answer to a private message sent to me:

Dimension statements let Visual Basic know that certain variables of certain types will be used in the program.

Dim appIMSI as IMSIGX.Application tells the program we will be using a variable named appIMSI and that the variable appIMSI will refer to an IMSIGX (turbocad) Application

Dim drDr As IMSIGX.Drawing  tells the program we will be using a variable named drDr and that the variable drDr will refer to an IMSIGX (turbocad) Drawing

Dim grGr As IMSIGX.Graphic  tells the program we will be using a variable named grGr and that the variable grGr will refer to an IMSIGX (turbocad) Graphic

Dim grsGrs As IMSIGX.Graphics  tells the program we will be using a variable named grsGrs and that the variable grsGrs will refer to an IMSIGX (turbocad) Graphics Collection


Set statements tells the program exactly what objects the variable does refer to.

Set appIMSI = GetObject(, "IMSIGX.Application") makes the variable appIMSI refer to the  IMSIGX (turbocad) Application

Set drDr = appIMSI.ActiveDrawing makes the variable drDr refer to the  IMSIGX (turbocad) Application Active Drawing

Set grsGrs = drDr.Graphics makes the variable grsGrs refer to the  Graphics Collection of the IMSIGX (turbocad)Application Active Drawing

Set grGr = grsGrs.AddLineSingle(1, 1, 1, 5, 5, 5) Adds a graphic named grGr to the graphics collection(grsGrs)

Jacobusdt,  Please note Grs in your code was changed to grsGrs.  In your code, the program has no idea what Grs is,  to let the program know what Grs is you need two statements.

1) Declare the variable Grs
    Dim Grs As IMSIGX.Graphics

2) Once you have declared what Grs will be, make it equal to something
    Set Grs = IMSIGX.ActiveDrawing.Graphics

Logged


* May 25, 2011, 06:39:32 AM
#6
I really appreciate the help on the forum.

I have a new problem and hope you would be able to help.

I have data that I would like to visualise in TurboCad.  The data has the form u = f(x,y,z).

The variables x, y and z are utilised by Excel solver to generate the result u.

I would like to display u as sphere of size u at the coordinates x,y,z.

For example.  At x=200, y=100 and z=50, the value of u=32.5 (Calculated).  I would like to use VBA in Excel to add a sphere with radius 16.25 (32/2) at position (200,100,50) and continue with all the values.  I calculated many values and there is just no way to manually do the visualisation to determine the sound opperting areas.  At least I hope it would not be necessary.

If I can add the spheres it will help greatly.
I cant find in the SDK or the Object browser any information to help me.

A further help would be to actually colour code the spheres based on their size.  This will give me a double visualisation effect - size and colour.

I will define a function to convert the size to a suitable RGB value to be used.

Hope you can help once again.

Thanks
Jacobus dt

Logged