Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Position a view on a sheet - random behavior issue

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Anonymous
472 Views, 6 Replies

Position a view on a sheet - random behavior issue

Hello,

 

I have this code that will scale a drawingview & then it will position it to the center of the sheet.

The behavior is most of the time OK, but sometimes the view just really out of the center. What is the reason for this?

What is also the difference between oDrawView.Center & oDrawView.Postion? They seam to do exactly the same. Could the issue be cause of this?

 

We almost always use a normal base view & a overlay (to highlight some parts)

 

See example of the view misplaced by this code:Image 1.pngThis is my code:

 

Public Sub AutoScale()
'Scale all views on all pages to fit nicely on the sheets

'step 0. declare stuff
  Dim oDrawDoc As DrawingDocument
  Dim oSheet As Sheet
  Dim oDrawView As DrawingView
  
  Set oDrawDoc = ThisApplication.ActiveDocument

' step 2. Process all pages
    Dim i As Integer
    i = 3
    For i = 3 To oDrawDoc.Sheets.Count
        Set oSheet = oDrawDoc.Sheets.Item(i)
        Set oDrawView = oSheet.DrawingViews.Item(1)
        Call ProcessPages(oSheet, oDrawView)
    Next

End Sub


Private Sub ProcessPages(ByVal oSheet As Sheet, ByVal oDrawView As DrawingView)
' step 1 get current drawingscale
    Dim oDrawViewScale As Double
    oDrawViewScale = oDrawView.Scale

' step 2 Determine the best scale by looking at the view widths.
    Dim dWidthScale As Double
    dWidthScale = (oSheet.Width - 5) / oDrawView.Width * oDrawViewScale    '5cm = distance to stay away from sides

' step 3 Determine the best scale by looking at the view heights.
    Dim dHeightScale As Double
    dHeightScale = (oSheet.Height - 9) / oDrawView.Height * oDrawViewScale    '9cm = distance to stay away from top and bottom (including the header)

' step 4 Choose the best scale
    Dim ComputeViewScale As Double
    If dWidthScale < dHeightScale Then
        ComputeViewScale = dWidthScale
    Else
        ComputeViewScale = dHeightScale
    End If

' step 5 set the correct scale
    oDrawView.ScaleString = ComputeViewScale

'step 6 set the location of the view
    Dim CenterPoint As Point2d
    'place above the center of the page (2cm)
    
' - - - - I believe it goes wrong here  - - - - '
' - - - - - - - -
' - - - - - - - -
' - - - - - - - -

    Set CenterPoint = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Width / 2, oSheet.Height / 2)
    oDrawView.Center = CenterPoint
    
' - - - - - - - -
' - - - - - - - -
' - - - - - - - -
' - - - - - - - -
' - - - - I believe it goes wrong here  - - - - '
    
    
End Sub

thank you all for your input!

 

 

6 REPLIES 6
Message 2 of 7
Owner2229
in reply to: Anonymous

From API help:

Center      Gets and sets the display center point of the drawing view on the owning sheet.

 

 

Position    Gets and sets the point used to position the drawing view on the sheet.

 

Po position the view you should use the ".Position" property. That should solve your issue.

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 7
Anonymous
in reply to: Owner2229

Hello,

 

I have tested my code with ".postion" a bit but this I keep encountering the non-predictable behavior.

 

 

 

I also see that depending the where you create your sketch (first click on a model or first click on the sheet) the scale of your sketch is different.

Maybe this has something to do with it? Maybe I need to put in a multiplicator or something?

 

example 1: 100mm in sketch based on view

example 1.png

 

 

 

 

 

 

 

 

 

 

 

 

 

Example 2: 100mm in sketch based on sheet
example 2.png

 

 

 

 

Message 4 of 7
MechMachineMan
in reply to: Anonymous

Depending on the API calls you are using, some of them are scaled by the view scale, so you may need to account for that in your code. I think oView.Height and oView.Width are both affected by this.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 5 of 7
Anonymous
in reply to: MechMachineMan

Hello,

 

I have Changed the code a bit for compensating the offset  by deducating ''the center-location of the view itself'' from the ''center of the page''.

The result is already better but still feels unpredictable. Especially if you run the macro multiple times.

Also the behavior of the zoom is not always uniform.

 

So I still believe I miss an important piece of insight in how Inventor works with this.

 

This is the improved code: 

 

Public Sub AutoScale()
'Scale all views on all pages to fit nicely on the sheets

'step 0. declare stuff
  Dim oDrawDoc As DrawingDocument
  Dim oSheet As Sheet
  Dim oDrawView As DrawingView
  
  Set oDrawDoc = ThisApplication.ActiveDocument

' step 2. Process all pages
    Dim i As Integer
    i = 3
    For i = 3 To oDrawDoc.Sheets.Count
        Set oSheet = oDrawDoc.Sheets.Item(i)
        Set oDrawView = oSheet.DrawingViews.Item(1)
        Call ProcessPages(oSheet, oDrawView)
    Next

End Sub


Private Sub ProcessPages(ByVal oSheet As Sheet, ByVal oDrawView As DrawingView)
' step 1 get current drawingscale
    Dim oDrawViewScale As Double
    oDrawViewScale = oDrawView.Scale

' step 2 Determine the best scale by looking at the view widths.
    Dim dWidthScale As Double
    dWidthScale = (oSheet.Width - 5) / oDrawView.Width * oDrawViewScale    '5cm = distance to stay away from sides

' step 3 Determine the best scale by looking at the view heights.
    Dim dHeightScale As Double
    dHeightScale = (oSheet.Height - 9) / oDrawView.Height * oDrawViewScale    '9cm = distance to stay away from top and bottom (including the header)

' step 4 Choose the best scale
    Dim ComputeViewScale As Double
    If dWidthScale < dHeightScale Then
        ComputeViewScale = dWidthScale
    Else
        ComputeViewScale = dHeightScale
    End If

' step 5 set the correct scale
    oDrawView.ScaleString = ComputeViewScale

'step 6 set the location of the view
    Dim PlacementPoint As Point2d
    'place above the center of the page (2cm)
    
' - - - - I changed things here  - - - - '
' - - - - - - - -
' - - - - - - - -
' - - - - - - - -

    Set PlacementPoint = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Width / 2 - oDrawView.Center.x, oSheet.Height / 2 - oDrawView.Center.y)
        '-> I compensate by deducting the centerlocation of the view from the center of the page
    oDrawView.Postion = PlacementPoint
        '-> I now use the '.Postion
        
    
' - - - - - - - -
' - - - - - - - -
' - - - - - - - -
' - - - - - - - -
' - - - - I changed things here  - - - - '
    
    
End Sub

 

Message 6 of 7
chandra.shekar.g
in reply to: Anonymous

Hi @Anonymous,

 

Try the following change in code.

 

Set CenterPoint = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Width / 2, oSheet.Height / 2)
oDrawView.Position = CenterPoint

Can you please provide drawing to test position?

 

Please feel free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 7 of 7
Anonymous
in reply to: chandra.shekar.g

Hello,

 

So after some more extended testing I believe I have it figured it out.

 

What I have learned: 

 

  1. You need to use the oDrawview.position instead of the oDrawview.center
  2. You need to compensate for the current Odrawview.center when determining the new positioning point
    Set PlacementPoint = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Width / 2 - oDrawView.Center.x, oSheet.Height / 2 - oDrawView.Center.y)
        '-> I compensate by deducting the centerlocation of the view from the center of the page
    oDrawView.Postion = PlacementPoint
        '-> I now use the '.Postion

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report