Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with VB Macro code 2

Status
Not open for further replies.

Tobin1

Petroleum
Nov 9, 2007
176
Howdy!

I’m just beginning to learn a bit about the SW API. In the past I used AutoLISP in AutoCAD for productivity enhancements. But I didn’t get into VB at the time. I’ve been using SW daily for a little over a year now. I downloaded a few nice SW Macro’s and recorded a couple of my own.

I’ve found a Macro (AddViewLabel) that is very close to something that would help me label views the way I need to more efficiently. AddViewLabel is a collaborative effort by Matthew Lorono and Regg. I contacted Mr. Lorono to discuss an addition to the code. He’s agreeable with trying to add something to the code.

I need to change the view label’s because, as I’m sure you are all aware, SW uses only one text size for both lines of the view label it produces. I need the last line (the scale) to be a smaller text size than the other lines.

I’d like to change the AddViewLabel's code so it will do the following:
1) Use the SW “Section View Text” text size setting for the first two lines the Macro creates (Firster and Seconder).
Currently the Macro uses the SW “Note” text size setting for all text size:
[get note annotation object]
[Set swAnnotation = swNote.GetAnnotation]
2) Use the SW “Note” text size setting for the third line the Macro creates (showme).
The Macro is currently set up to use this text size for all text.

As I said I’m a beginner at this. I can look thru the code and figure out some of what is going on but not enough to figure out how to add the above function.

Can anybody help me with this?


 
Replies continue below

Recommended for you

It sounds like you are wanting to create a single note with different font sizes in it. That will require the note to be a rich-text containing note. A rich-text note is what you end up with if you manually create a note, then select a certain portion of the text of the note and change the font or font size in the pop-up text editing toolbar. This adds invisible text tags to the contents of the note that affect how the text is displayed. Doing this clears the checkbox for "Use Document Font" in the note's Property Manager. However, you can do the same by manually typing in these text tags to the body of a note, and the "Use Document Font" setting will be unaffected. For example, edit any note, put the cursor somewhere in the middle of the note, and type:

<FONT size=40>

(or some other more reasonable number). When you type that closing bracket ">", that text and brackets will disappear and all the following note text will change to size 40.

This is going to be basically what you want to do programmatically. In this macro, the note is actually created and added to the drawing in the Definer() subroutine using the command "InsertNote." The note text is the argument of that function. You will need to add the size tags into the text of the note being created. Since it is a note, it defaults to using the document note size setting. The simplest way to change the font size would be to just hard-code in the size values inside the size tags. It would be possible for the macro to read the document settings to determine the Section View Text and Note settings. However, actually reading in those values is probably a bit beyond where you are right now in your VBA/SW API experience. Let's just say you want the first and second lines to be 10 and the last line to be 5. You would modify each of the InsertNote commands in the Definer subroutine similar to:

Code:
'Original code line:
Set swNote = Part.InsertNote(FieldFirst & Chr$(10) & FieldSecond & Chr$(10) & "SCALE: " & ScaleRatio(0) & ":" & ScaleRatio(1))

'New code line:
Set swNote = Part.InsertNote("<FONT size=10>" & FieldFirst & Chr$(10) & FieldSecond & Chr$(10) & "<FONT size=5>SCALE: " & ScaleRatio(0) & ":" & ScaleRatio(1))

All the note text will use the default settings except where modified by a text tag. All text after a tag is affected by that tag until changed by some new tag. Tags can change just about any aspect of a note. The tags do not actually affect the "Use Document Font" setting of the note. To remove all the tags and go back to default, just uncheck the box for "Use Document Font", deselect the note, reselect the note, and recheck the box.
 
handleman,

Thanks for the help!

This does help me to understand the code a little better. I will have to figure out how to make the Macro use the SW Note & Section View Label text. There's one more good suggestion from Mr. Lorono that needs to be added also. But this is very helpful to me.

I know I have a lot of basics to learn. I've downloaded a copy of Visual Basics Express and started reading thru the lessons.

Possibly you could answer a couple of questions, then I'll leave you alone or start a new post. :)

1) Could you point me to the location of a list of "invisible text tags" and related items?

2) In the AddViewLabel code what does <Chr$(10)> mean?

Thanks Again
 
I don't really have a list of text tags. "Text tags" is probably not the right term either. It's just what I'm trying to call the notation SW uses to denote special characters, symbols, special values, rich text formatting, etc.

Take, for example, a diameter dimension. When you create a diameter dimension in a drawing, the drawing shows the dimension as "Ø50". If you look in the property manager, the text it shows is "<MOD-DIAM><DIM>". Those expressions with "<>" are what I'm calling "tags", because they're sort of similar to HTML tags. If you want to investigate these "tags" further, you can use this macro:
Code:
Sub main()
Dim OutPut As String
OutPut = "Displayed text:" & vbCrLf
OutPut = OutPut & Application.SldWorks.ActiveDoc.SelectionManager.GetSelectedObject6(1, -1).GetText
OutPut = OutPut & vbCrLf & vbCrLf & "Actual text:" & vbCrLf
OutPut = OutPut & Application.SldWorks.ActiveDoc.SelectionManager.GetSelectedObject6(1, -1).PropertyLinkedText
MsgBox OutPut
End Sub

To use this macro, create a note on a drawing. Then change portions of the note. Don't change the entire note using the Property Manager, use the popup text editing toolbar. Change fonts, font size, etc. for portions of the note. Add bullets/numbering. Insert symbols. Do whatever you want to see tags for. Then select the note and run the macro. You will see a message box that will match (as much as possible) the text as displayed on the screen along with the actual text that SW stores inside that note. That text contains the "tags" that instruct SW on how to actually display the note. The macro will error out if you do not have a note selected prior to running.

The Chr() function returns the character corresponding to the function argument in ASCII. Chr(10) is a linefeed. In this case it works the same as Chr(13) or vbCrLf - makes a string display as multiple lines on screen.

Unless you do lots of different drawings in which the Section View text size is different all the time, you can just use the <FONT size=X> tag, where X is whatever size fits your company standard for section view text size. You don't have to make the macro figure out the document setting for section view text size.
 
handleman,

Thanks so much for your response!

I still remember some of the AutoLISP text attribute commands (%%C, %%D, %%U, %%P, etc.). Unfortunately they are probably permanently etched in my brain, but not used much anymore. :)

Your Macro for extracting these from SW is very helpful. Just examining it helps me understand a little more about how to write my own VB code.

Apparently <Chr$(10)> is a little over my head still. I was just reading about arrays yesterday. I remember arrays from learning the REXX language many years ago. I'll keep reading. :)

Yes I can change the test size by changing the code, but, others in the company might be interested in this AddViewLabel tool. Even just for myself - I'd like to do this for a productivity enhancer. The least amount of fiddling around the better. :)

Thanks Again
 
Chr(10) has nothing to do with arrays. It is a VBA function that returns the character corresponding to the ASCII value in parenthesis. Chr(65) returns "A" because 65 is the ASCII value of the capital "A". Chr(51) returns "3". Here, Chr(10) is used to insert a linefeed into the string, because 10 is the ASCII value of a linefeed. Look up the Chr() function in VBA help. If you want a string variable to contain multiple lines, like:

First line
Second line
Third line

you can't just put those directly in the code like:

Code:
myStringVariable = "First line
Second line
Third line"

because each new line in the code window is interpreted as just that - a new line of code that must contain valid instructions. You have to use some other way to get those newlines into the string. This code uses Chr(10) as the carriage return:

Code:
myStringVariable = "First line" & Chr(10) & "Second line" & Chr(10) & "Third line"

The "&" symbol is the string contatenation operator. The "+" sign usually works too, but since it's also the addition operator you will rarely get unexpected results. It's best to use "&" when you have two different strings you want to stick together.
 
Tobin1,
Please let me know when you finish this Macro. I would love to get a copy of it.

Best,


Colin Fitzpatrick (aka Macduff)
Mechanical Designer
Solidworks 2007 SP 5.0
Dell 490 XP Pro SP 2
Xeon CPU 3.00 GHz 3.00 GB of RAM
nVida Quadro FX 3450 512 MB
I'm just a little verklempt. Talk amongst yourselves. I'll give you a topic. Pink Floyd, was neither Pink nor Floyd. Discuss!--“Coffee Talk” Mike Myers SNL
 
If you really, really want the macro to use those specific text heights, the following lines of code will load those sizes into variables:

Code:
Dim SecViewLabHt As Double
Dim NoteHt As Double

SecViewLabHt = Application.SldWorks.ActiveDoc.GetUserPreferenceTextFormat(swDetailingSectionLabelTextFormat).CharHeight
NoteHt = Application.SldWorks.ActiveDoc.GetUserPreferenceTextFormat(swDetailingNoteTextFormat).CharHeight

You can then use these variables in place of hard-coding the numbers in. Using the same example line I posted before:


Code:
'New code line:
Set swNote = Part.InsertNote("<FONT size=" & SecViewLabHt & ">" & FieldFirst & Chr$(10) & FieldSecond & Chr$(10) & "<FONT size=" & NoteHt & ">SCALE: " & ScaleRatio(0) & ":" & ScaleRatio(1))

Each of those "Set swNote = Part.Inse......" lines corresponds to a different combination of the options on the form. You will have to modify each InsertNote command to size the notes the way you want them.
 
handleman,

OOHH!! Definitely got ya now. Chr$(10) is ASCII for a carriage return (linefeed). I probably won't forget that one for a while. :) Thanks for explaining that.

As for your second post I'll have to let it sink in a bit. I know I have to place this in just the right place in the code. I'll let you know when I figure it out. :)


macduf,

I'd be glad to let you know when we finish this.



 
Very nice Tobin1!

Colin Fitzpatrick (aka Macduff)
Mechanical Designer
Solidworks 2007 SP 5.0
Dell 490 XP Pro SP 2
Xeon CPU 3.00 GHz 3.00 GB of RAM
nVida Quadro FX 3450 512 MB
I'm just a little verklempt. Talk amongst yourselves. I'll give you a topic. Pink Floyd, was neither Pink nor Floyd. Discuss!--“Coffee Talk” Mike Myers SNL
 
My generic advice to all newbies: learn VBA first. Do some Excel VBA project tutorials and get a feel for the language. It helps a lot.
 
TheTick,

That sounds good, I was actually thinking of projects with a database. Do you think that could be just as helpful?



 
macduff

Attached is the SW Macro I’ve been working on. If you’re interested in beta testing give it a try and let me know of any issues.

This is my first real try at a SW Macro of this kind so I’m sure it could be “cleaned up” some. Let me know what you think.

Thanks




Tobin Sparks
www.nov.com
 
 http://files.engineering.com/getfile.aspx?folder=a0aceaf5-ed3c-4554-9fb1-123747d8d18f&file=Label_View.zip
OMG, this is awesome Tobin! A star for you!

One thing....can you link the font back to the Section or a Detail, if you change it? I send a jpg in a moment.

Colin Fitzpatrick (aka Macduff)
Mechanical Designer
Solidworks 2007 SP 5.0
Dell 490 XP Pro SP 2
Xeon CPU 3.00 GHz 3.00 GB of RAM
nVida Quadro FX 3450 512 MB
I'm just a little verklempt. Talk amongst yourselves. I'll give you a topic. Pink Floyd, was neither Pink nor Floyd. Discuss!--“Coffee Talk” Mike Myers SNL
 
I always ask, or as least make sure permission is granted by the type of release (freeware/public domain/distribute freely). E-mail me if you have concerns about anything on the site.

Please note that I have a standing permission from a some individuals.


Matt Lorono
CAD Engineer/ECN Analyst
Silicon Valley, CA
Lorono's SolidWorks Resources
Co-moderator of Solidworks Yahoo! Group
and Mechnical.Engineering Yahoo! Group
 
macduff

No this won't link to the settings. It's just a "dumb" note. To make it link back to the settings would be a whole different kind of Macro. Not sure if you could even call that a Macro :) .

Thanks

Tobin Sparks
 
You almost always ask. No one has permission to redistribute my stuff on other websites. I've never given it to anyone. Links to my site are fine. Taking my goods and selling them in another store is not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor