hardwired Posted August 24, 2012 Posted August 24, 2012 Hey all, I appreciate this is more VB than VBA but its an AutoCAD tool i'm making so posting it here too.. Trying to creating a tool that inserts datum lines in a drawing which is fine, the only issue is the formatting of the attribute to display within the block (see the bold part of the code below). I'm trying to display the datum level as "+0.000" which is fine for most input but if the number has any trailing zeros, they are not displayed. I've searched high and low on the net for this and have tried everything i have found but still no results.. (All variables in the code have been declared and no errors occur at run-time, just not the correct display: ie: "+6.800" displays in the block as "+6.8" and "+8.760" displays as "+8.76") '************************************************* '************ GROUND FLOOR *************** '************************************************* GROUND: ' Check if GROUND is selected.. If LVLtxtGRND.Text = "" Then GoTo FIRST Else 'If a value is shown then insert the block.. LVLvalueGRND = LVLtxtGRND.Text ' Draw LINE for level.. LineGRNDstart(0) = 0: LineGRNDstart(1) = LVLvalueGRND: LineGRNDstart(2) = 0 LineGRNDend(0) = LINEend: LineGRNDend(1) = LVLvalueGRND: LineGRNDend(2) = 0 Set LINEgrnd = ThisDrawing.ModelSpace.AddLine(LineGRNDstart, LineGRNDend) LINEgrnd.Layer = "X-Levels" ' Insert LEVEL Block.. LVLinsertGRND(0) = 0: LVLinsertGRND(1) = LVLvalueGRND: LVLinsertGRND(2) = 0 Set LVLblockGRND = ThisDrawing.ModelSpace.InsertBlock(LVLinsertGRND, "Level", 250#, 250#, 250#, 0) LVLblockGRND.Layer = "X-Notes" [b]' Display level figure on screen.. LVLvalueGRND = LVLvalueGRND / 1000 'Get it into metres, no mm.. LVLvalueGRND = Round(LVLvalueGRND, 3) LVLvalueGRND = Format(LVLvalueGRND, "0.000")[/b] AttribZ = LVLblockGRND.GetAttributes ' Get Block attributes.. For CountX = LBound(AttribZ) To UBound(AttribZ) Select Case AttribZ(CountX).TagString Case "LEVEL" AttribZ(CountX).TextString = PlusChar & LVLvalueGRND & " " & " " & DESCcomboGRND.Text End Select Next CountX 'On Error Resume Next ' MsgBox Err.Number & vbCr & Err.Description, vbExclamation, "Error:" End If '************************************************* '************ GROUND FLOOR *************** '************************************************* Any ideas? This is twisting my melon now grrr Quote
hugha Posted August 25, 2012 Posted August 25, 2012 Baffling. Shouldn't need to do this but as a real kludge until something better comes along you could compare the INSTR position of the decimal point with the string's length and append any needed zeroes. If no decimal point append ".000" to the string. Quote
Tyke Posted August 25, 2012 Posted August 25, 2012 LVLvalueGRND = Format(LVLvalueGRND, "0.000") To get the trailing zeros you need to rewrite the above line so it reads: LVLvalueGRND = Format(LVLvalueGRND, "0.##0") I hope that helps Quote
hugha Posted August 25, 2012 Posted August 25, 2012 To get the trailing zeros you need to rewrite the above line so it reads: LVLvalueGRND = Format(LVLvalueGRND, "0.##0") I hope that helps If that works we all learn something. I hope someone can explain why it should work, either by example or by arguing from first principles. Quote
Tyke Posted August 25, 2012 Posted August 25, 2012 I've used it a lot for this very situation in both VBA and VB .NET and it works fine. It was a real pain in the backside for me until I found the solution try it out and get back to us with your result. Quote
hugha Posted August 26, 2012 Posted August 26, 2012 It works for sure. Sorry, I was just idly curious as to how you came upon the answer - it's not well documented by MS e.g: http://msdn.microsoft.com/en-us/library/4fb56f4y(v=vs.90).aspx Quote
Tyke Posted August 26, 2012 Posted August 26, 2012 Glad it worked for you hugha. You are right it is not very well documented and I saw a similar link to the one you just posted, after which I just experimented a bit till I got it to work. The # symbol is a standard place holder in Visual Basic, although the documentation says that "0.000" should work, but it never did for me. I have been using it regularly now for around six years. Let's hope that it works for hardwired too Thanks for getting back hugha and If that works we all learn something. that we have all learned a little bit. Quote
hardwired Posted August 28, 2012 Author Posted August 28, 2012 Hey all, Thanks for all your replies. I tried the '0.##0' solution, Tyke but still no luck. I think my PC doesn't like to be too negative haha. This is a real head-screwer as it seems to be working for everyone else just not me. I'll give your first solution a go, hugha and see what happens Quote
Tyke Posted August 28, 2012 Posted August 28, 2012 I've tried out exactly the code you have on two computers, in AutoCAD 2010 and 2013 in both German and English language versions of AutoCAD, but admittedly only on a German Windows XP operating system. It works fine with your unaltered code and also with my suggestion. What OS do you have? 32 bit or 64 bit? It shouldn't make any difference, but check the decimal seperator in your Regional Settings. Quote
hardwired Posted August 30, 2012 Author Posted August 30, 2012 Hey Tyke, I've had a look at my regional settings and can't see anything else to change except figures to 3 decimal places but that didn't work anyway.. I'm on AutoCAD 2010 on an 32-bit XP Pro machine by the way.. Quote
Tyke Posted August 30, 2012 Posted August 30, 2012 We sometimes get a conflict here as the decimal seperator on a German language OS is by default a comma and some software will only work properly when its set to a "." I don't honestly believe it is your problem here, but its worth a look. Have you tried running your program on another machine? Try to find out if its machine specific or code specific. As an aside, I had exactly this problem in 2007 when I wrote some VBA code and used the "0.##0" to get round the problem. Yesterday I ran the same code on a new W7 x64 machine with AutoCAD 2013 and replaced "0.##0" with "0.000" and it worked fine! It looks to be more Machine specific than anything else. After I got that result I tested on anther XP Pro x86 running AutoCAD 2010 and "0.000" worked fine there too. Quote
hardwired Posted September 7, 2012 Author Posted September 7, 2012 Hey all, after being booted off this site for apparently sending an advert post, I'm back I managed to sort my number formatting / decimal place issue with a little bit of cheating though I'm afraid. I simply converted the value to a string variable then formatted it. Its fine for what I needed but obviously not the correct way but needs must I guess.. Thanks for all your help guys and if you find a reason why mine didn't work before, please let me know and I'll do likewise.. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.