• No results found

Display Document Information

Now we have gathered all this useful information, it would be nice to actually see it. We start by creating a multiline textbox to put the information into, and then simply add all of the information separated by a new line for each item.

In the .Net languages we already have a form created automatically for us, and if you are using the template from the first chapter to run this code, we already have a button, so all that is left is to add a textbox. Go to the Form Designer by double-clicking the Form1.cs file in the Solution Explorer. Now go to the Toolbox to the left and this time instead of dragging a Button object on to our form we are going to drag a TextBox object on.

Now drag the textbox to any size you would like and position it as such. Then making sure it is still selected, go to the Properties Window to the right, and set the following properties:

And here is what my form looks like:

Now switch back to the coding of the form. Within the buttons event handler function, we write the code to connect to SolidWorks and to get the active document code, and then all of the code we have just been writing above. It should all make sense if you have been

following along, and the only thing different is that instead of storing the information in separate variables, we have simply made one string variable that stores all of the values, separated by a new line.

C#

MessageBox.Show("Error getting SolidWorks Handle");

return;

}

swModel = (ModelDoc2)swApp.ActiveDoc;

if (swModel == null) {

MessageBox.Show("Failed to get active document");

return;

}

string nl = System.Environment.NewLine;

string strInfo = "File Information" + nl;

string fullpath = swModel.GetPathName();

strInfo += "Full Path: " + fullpath + nl;

if (fullpath != string.Empty) {

strInfo += "Directory: " + Path.GetDirectoryName(fullpath) + nl;

strInfo += "Filename: " + Path.GetFileNameWithoutExtension(fullpath) + nl;

strInfo += "Extension: " + Path.GetExtension(fullpath) + nl;

}

strInfo += "Title: " + swModel.GetTitle() + nl;

strInfo += "File Type: " +

((swDocumentTypes_e)swModel.GetType()).ToString() + nl;

strInfo += "Read-only: " + swModel.IsOpenedReadOnly().ToString() + nl;

strInfo += "View-only: " + swModel.IsOpenedViewOnly().ToString() + nl;

strInfo += "Large Assembly Mode: " +

swModel.LargeAssemblyMode.ToString() + nl;

strInfo += "Material Name: " + swModel.MaterialUserName + nl;

strInfo += "Material ID: " + swModel.MaterialIdName + nl;

strInfo += "Configuration Names" + nl;

string[] configNames = (string[])swModel.GetConfigurationNames();

foreach (string configname in configNames) {

strInfo += configname + nl;

}

strInfo += "Summary Info" + nl;

string[] summaryNames =

Enum.GetNames(typeof(swSummInfoField_e));

foreach (string summaryName in summaryNames) {

int sumID = (int)Enum.Parse(typeof(swSummInfoField_e), summaryName);

string summaryValue = swModel.get_SummaryInfo(sumID);

strInfo += summaryName + ": " + summaryValue + nl;

}

textBox1.Clear();

textBox1.Text = strInfo;

Any here is the finished result when you run the code:

For VBA we do much the same thing; start by going to Insert->User Form within the VBE. Then much like VS just drag a

CommandButton and a TextBox onto the form and from the properties on the left, set the TextBox property MultiLine to True, and change the Caption property of the button to whatever you would like the button to say.

Now double-click the button to create the event handler for the click.

Like VS we will place all of our code in here, and modify the module containing the main() function to simply open our form.

Start by editing the Macro1 file module by double-clicking it in the Project Explorer.

Within the main() function place the following code to

open the form upon running of the macro.

Sub main() UserForm1.Show End Sub

If you left the form Name property as default, then it will be called UserForm1. If you do not get the IntelliSense menu popping up when you press the period key after its name and before typing Show, then you have the wrong name, so check the name property of the form in the properties of the form designer.

Below this main subroutine we can place the LastIndexOf function we created earlier, or we can put this in the form code. I have placed it below the main for clarity.

Function LastIndexOf(stringToSearch As String, searchFor As String) As Integer

Dim iPos As Integer Dim iTemp As Integer iPos = -1

iTemp = 0

Do Until iPos = 0

If iPos = -1 Then iPos = 0

iPos = InStr(iPos + 1, stringToSearch, searchFor) If iPos <> 0 Then iTemp = iPos

Loop

iPos = iTemp - 1 LastIndexOf = iPos End Function

Now it s time to place all of our code for getting the information that we wrote above into the event handler function of the button that was created earlier, so that the information will be shown in the textbox when the

user clicks the button. To get back to the coding window with the button event handler just right-click the

UserForm1 item in the Project

Explorer and select View Code.

VBA

Dim strInfo As String

Private Sub CommandButton1_Click()

Set swApp = GetObject("", "SldWorks.Application") If swApp Is Nothing Then

MsgBox "Error gettings SolidWorks Handle"

Exit Sub End If

Set swModel = swApp.ActiveDoc If swModel Is Nothing Then

MsgBox "Failed to get active document"

Exit Sub End If

Dim fullname As String Dim filenamewithext As String Dim filelocation As String AddToStr "File Information"

fullname = swModel.GetPathName() AddToStr "Full Path: " & fullname If fullname <> "" Then

filelocation = Left(fullname, LastIndexOf(fullname, "\")) AddToStr "Directory: " & filelocation

filenamewithext = Right(fullname, Len(fullname) - Len(filelocation) - 1) AddToStr "Filename: " & Left(filenamewithext,

LastIndexOf(filenamewithext, "."))

AddToStr "Extension: " & Right(fullname, Len(fullname) - LastIndexOf(fullname, "."))

End If

AddToStr "Title: " & swModel.GetTitle()

AddToStr "File Type: " & swModel.GetType()

AddToStr "Read-only View: " & swModel.IsOpenedReadOnly() AddToStr "View-only View: " & swModel.IsOpenedViewOnly() AddToStr "Large Assembly Mode: " & swModel.LargeAssemblyMode

AddToStr "Material Name: " & swModel.MaterialUserName AddToStr "Material ID: " & swModel.MaterialIdName

AddToStr "Configuration Names"

configNames = swModel.GetConfigurationNames()

Dim configname As Variant

For Each configname In configNames AddToStr CStr(configname) Next

AddToStr "Summary Information"

AddToStr "Title: " & swModel.summaryinfo(swSumInfoTitle) AddToStr "Subject: " & swModel.summaryinfo(swSumInfoSubject) AddToStr "Author: " & swModel.summaryinfo(swSumInfoAuthor) AddToStr "Keywords: " & swModel.summaryinfo(swSumInfoKeywords) AddToStr "Comment: " & swModel.summaryinfo(swSumInfoComment) AddToStr "Saved By: " & swModel.summaryinfo(swSumInfoSavedBy) AddToStr "Creation Date: " &

swModel.summaryinfo(swSumInfoCreateDate)

AddToStr "Save Date: " & swModel.summaryinfo(swSumInfoSaveDate) AddToStr "Creation Date 2: " &

swModel.summaryinfo(swSumInfoCreateDate2)

AddToStr "Save Date 2: " &

swModel.summaryinfo(swSumInfoSaveDate2)

TextBox1.Text = ""

TextBox1.Text = strInfo End Sub

Sub AddToStr(info As String) strInfo = strInfo & info & vbCr End Sub

* Find the complete C#, VBA and VB.Net examples on the CD if you have any troubles.

Related documents