Automatic Scale and View Position

Automatic Scale and View Position

Anonymous
Not applicable
11,034 Views
11 Replies
Message 1 of 12

Automatic Scale and View Position

Anonymous
Not applicable

Hi! 

I made an iLogic code that automatically scales the views to fit the sheet and moves them to the correct place. I use this when I have parametric parts that change size from small to big and them don't fit the sheet. 

 

As specified in the code the views must follow a specific order and name in the sheet:
view.png

The pattern view name should be this one, but it's not needed to have all views, it works from 1 up to 5 views automatically.

 

 

It's working just fine for me. But any improvements that i can had? any ideas to make it better?

 

 

 

' Automatic Scale and Position for Autodesk Inventor Drawings.
' V1.0 - 2015-08-24

' Views should follow an specific order and name.

'Views should follow this names and position but it's not needed to have them all! It works from only 1 view up to all 5 views.
'If they are not there the program will ignore.

'User request play in YY & YY axis
	'Play in relation to the sheet size. It allows space for the Title Block and other notes if needed.
SheetPlayX = 100 
SheetPlayY = 100 

'User Fine Tune - In this you can fine tune some distances.
	'Deviation X will move all views to right if positive or to left if negative.
	'Deviation Y will move all views up if positive or down if negative. Normally used to move views up when there is a title block in the bottom.

deviation_X = 0
deviation_Y = 0
play_between_views = 0 

'Define variables
Dim View(0 To 5) As String
Dim CenterX(0 To 5) As Single
Dim CenterY(0 To 5) As Single
Dim ViewHeight(0 To 5) As Single
Dim ViewWidth(0 To 5) As Single
Dim PlayX(0 To 3) As Single
Dim PlayY(0 To 3) As Single
ViewQtX = 0
ViewQtY = 0

'Determine the maximum available space in the sheet subtracting the play in x&Y and between views requested by the user.
SheetWidth = ActiveSheet.Width - SheetPlayX - play_between_views
SheetHeight = ActiveSheet.Height - SheetPlayY - play_between_views

'Define the name of each view.
View(1)="VIEW1"
View(2)="VIEW2"
View(3)="VIEW3"
View(4)="VIEW4"
View(5)="VIEW5"

'Determines the current scale of the main view (VIEW 1). It only reads the main one because it assumes that all other views are in the same scale.
CurrentScale=ActiveSheet.View(View(1)).Scale

' Try to find the width and height of each view in scale 1:1. If not found instead of give an error, makes it equal to zero.
For i=1 To 5
Try
ViewHeight(i)=ActiveSheet.View(View(i)).Height*(1/CurrentScale)
ViewWidth(i)=ActiveSheet.View(View(i)).Width*(1/CurrentScale)
Catch
ViewHeight(i)=0
ViewWidth(i)=0
End Try
Next

'Determines the max width and height that all views occupy in scale 1:1
MaxWidth = ViewWidth(5) + ViewWidth(1) + ViewWidth(3)
MaxHeight = ViewHeight(4) + ViewHeight(1) + ViewHeight(2)


'Width maximum height/width and maximum sheet size determine what scale is needed to fit in the sheet.
ScaleX =  SheetWidth / MaxWidth 
ScaleY =  SheetHeight / MaxHeight

FinalScale = Min(ScaleY,ScaleX)

'Apply the final scale to the Main view rounded to the nearest .25
If FinalScale <1 Then
FinalScale = FinalScale^-1
inc = .25
FinalScale = Round(Round(FinalScale,2) / inc) * inc
ActiveSheet.View(View(1)).ScaleString="1/"&FinalScale
Else
inc = .25
FinalScale = Round(Round(FinalScale,2) / inc) * inc
ActiveSheet.View(View(1)).Scale=FinalScale
End If

'Writes to an custom iPropertie the scale
iProperties.Value("Custom", "Scale")=ActiveSheet.View(View(1)).ScaleString

'Reads the new height and width of each view.
For i=1 To 5
Try
ViewHeight(i)=ActiveSheet.View(View(i)).Height
ViewWidth(i)=ActiveSheet.View(View(i)).Width
Catch
ViewHeight(i)=0
ViewWidth(i)=0
End Try
Next

'Determine the new maximum width and height occupied by all views in the current scale.
MaxWidth = ViewWidth(5) + ViewWidth(1) + ViewWidth(3)
MaxHeight = ViewHeight(4) + ViewHeight(1) + ViewHeight(2)

'Determine how many rows and columns of views there are.
If ViewWidth(5)<>0 Then
NumberViewsX = NumberViewsX+1
End If
If ViewWidth(1)<>0 Then
NumberViewsX = NumberViewsX+1
End If
If ViewWidth(3)<>0 Then
NumberViewsX = NumberViewsX+1
End If

If ViewHeight(4)<>0 Then
NumberViewsY = NumberViewsY+1
End If
If ViewHeight(1)<>0 Then
NumberViewsY = NumberViewsY+1
End If
If ViewHeight(2)<>0 Then
NumberViewsY = NumberViewsY+1
End If


'Width the new views width and height determine the available space in the sheet and divides by the number of views + 1/
'to determine how much play can be left between views. To that play also adds the user requested play_between_views.
PlayX(0) = (ActiveSheet.Width - MaxWidth) /(NumberViewsX+1)
PlayY(0) = (ActiveSheet.Height - MaxHeight) /(NumberViewsY+1)

If ViewWidth(5)<>0 Then
PlayX(1) = PlayX(0) + play_between_views
End If
If ViewWidth(1)<>0 Then
PlayX(2) = PlayX(0) + play_between_views
End If

If ViewHeight(2)<>0 Then
PlayY(1)=PlayY(0) + play_between_views
End If
If ViewHeight(1)<>0 Then
PlayY(2)=PlayY(0) + play_between_views
End If

'Defines the all views position. By default they are centred and equally spaced, but if the user adds any X/Y/view play it will relocated accordingly.
CenterX(5)= PlayX(0) + deviation_X + ViewWidth(5)/2
CenterX(1)= CenterX(5) + ViewWidth(5)/2 + PlayX(1) + ViewWidth(1)/2
CenterX(3)= CenterX(1) + ViewWidth(1)/2 + PlayX(2) + ViewWidth(3)/2

CenterX(2)= CenterX(1)
CenterX(4)= CenterX(1)

CenterY(2)= PlayY(0) + deviation_Y + ViewHeight(2)/2
CenterY(1)= CenterY(2) + ViewHeight(2)/2 + PlayY(1) + ViewHeight(1)/2
CenterY(4)= CenterY(1) + ViewHeight(1)/2 + PlayY(2) + ViewHeight(4)/2

CenterY(5)=CenterY(1)
CenterY(3)=CenterY(1)

'Apply to all views the new location
For i=1 To 5
Try
ActiveSheet.View(View(i)).SetCenter(CenterX(i),CenterY(i))
Catch
End Try
Next

'Update Inventor Drawing
InventorVb.DocumentUpdate()

'(c)2015 Fabmsg




 

 

11,035 Views
11 Replies
Replies (11)
Message 2 of 12

PaulMunford
Community Manager
Community Manager
Great stuff!

This guy could use your help.

http://forums.autodesk.com/t5/inventor-ideastation/automated-view-scale-and-position/idc-p/5785327#M...


Customer Adoption Specialist: Autodesk Informed Design
Help | Learn | Forum | Blog | Ideas | Sample content | Linkedin 

0 Likes
Message 3 of 12

Anonymous
Not applicable
That guy is me 😄 I posted as an Idea because if it's integrated in inventor could work much much better..
Message 4 of 12

Anonymous
Not applicable

Hi,

 

SyntaxEditor Code Snippet

'Apply the final scale to the Main view

I think there is something wrong with this line:

ActiveSheet.View(View(1)).ScaleString="1/"&FinalScale

I'm getting this error:

Error in rule: Rule0, in document: test.idw

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

 

 

 

0 Likes
Message 5 of 12

johnsonshiue
Community Manager
Community Manager

Hi! Please attach the drawing file you are working on here. It would be a lot easier to tell where the problem is.

Many thanks!



Johnson Shiue (johnson.shiue@autodesk.com)
Software Test Engineer
0 Likes
Message 6 of 12

Anonymous
Not applicable

I have attached the idw and ipt files.

 

Thank you!!

Message 7 of 12

Anonymous
Not applicable

I have the same error - any suggestions ??

0 Likes
Message 8 of 12

marcin_otręba
Advisor
Advisor

I do it differently:

 

1 . on save drawing i for each view add iproperties to drawing named view_name in wich i store position of view and size of it.

2. when i change dimmensions of part i run rule on that event that will rescale draiwing views according to correspond iproperties.

 

i think you should consider to do it this way because it will wor for all kinds of drawings like 1 or multiple sheets, and 1 or multiple views on each sheet.

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 9 of 12

Anonymous
Not applicable

Hi 

I've just tried running this code and I get one error message.

I've added the code to my part which has a rule automatically generating a drawing template.

I have now added this code as another rule to my part SQ-Rect Silencer.ipt.

 

The error message I am getting is:-

Error in rule: Rule 23, in document: SQ-Rect Silencers.ipt

ThisDrawing: This document "SQ-Rect Silencers" is not a drawing

 

This doesn't appear in the code?

May be a daft question but should this code run within the ipt part or the automatically generated idw drawing?

Either way it doesn't appear to generate any views on the idw drawing. I only need a base and projected view with for dimensions.

 

Thanks for any help 

0 Likes
Message 10 of 12

Anonymous
Not applicable

If you're using anything before Inventor 2019 you can't run a rule that uses "ActiveSheet" from anything other than a drawing which is what causes your error.  This rule also doesn't create any drawings just resizes them.

0 Likes
Message 11 of 12

geludegijon
Explorer
Explorer

I'm learning iLogic and want to achieve something similar. It is possible for you to share the code? Would be a huge help for me and sure also for others.

Message 12 of 12

thomas_m
Observer
Observer

Hi @marcin_otręba ,

 

This sounds interesting, would you be able to share a bit more detail on how you do this?

 

Thanks,

 

Thomas