Create Viewing by VBA

Create Viewing by VBA

Anonymous
Not applicable
3,659 Views
19 Replies
Message 1 of 20

Create Viewing by VBA

Anonymous
Not applicable

Hello,
please take some consideration for me, because I am not a programmer.
I want to create a VBA code to create a drawing view.

Drawing with sheet is done and view insert with front and standard.

 

What I need:
- Change the view to backside

- Change the type of view from standard to a view called "machine"

- Change Position of the View to the bottemleft corner

- Set scale to 1:1

 

And thats for every sheet with the name "Machine".

Where can I finde information for the points? Easy to understand. 😉

Maybe someone can help with a code?

 

Thx for your help!

0 Likes
Accepted solutions (1)
3,660 Views
19 Replies
Replies (19)
Message 2 of 20

Anonymous
Not applicable

As I managed to do the other codes in iLogic, how can I rotate a View (change camera to backside, left, right, bottom, top)?

0 Likes
Message 3 of 20

HermJan.Otterman
Advisor
Advisor

look in the help of inventor for Programming/Api help.

 

there are VBA examples on how to create views on a sheet 

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


Message 4 of 20

Anonymous
Not applicable


Ok. Got it.
Exept one point. 😞

 

- Change Position of the View to the bottemleft corner

 

In iLogic its simple:

ActiveSheet.View(View).SetSpacingToCorner(0, 0, SheetCorner.BottomLeft)

Where can I find information how to set the view to the bottomleft corner via VBA?
A direkt link, keyword or example would be helpfull.

0 Likes
Message 5 of 20

HermJan.Otterman
Advisor
Advisor

here is some VB code:

 

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument

Dim oView As DrawingView = Nothing

For Each oView In oDrawDoc.ActiveSheet.DrawingViews

If oView.Name = "VIEW1" Then

Exit For

End If

Next

Dim viewCenter As Point2d = oView.Center

Dim viewpostition As Point2d = oView.Position

 

you can change the X and Y of these points

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


Message 6 of 20

Anonymous
Not applicable

THX for your help.
But the code is old or vb.net? As I told, I am not a programmer. I can work with some code and edit it, change it. But I think that there isnt any code already for it... Smiley Sad

I found a "Automation" for a new drawing. But its old and much too complicted for me, because its too complex at all.

 

What I want to create is ...

 

- Add a new Sheet called "MachineSheet" *

- Resize Sheet to custom size for machinesize *

- Select the IAM from FirstSheet

- Add new View with IAM from FirstSheet in MachineSheet

- point new View to the left bottom corner exact to 0:0 with its left corner

- scale 1:1 *

- change view options *

 

* = already done

 

newview.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 Likes
Message 7 of 20

Anonymous
Not applicable

Is it correct, that I have to calculate the left corner of the view out of its size?
The left bottom corner of the sheet,too.

And then point the calculated view point onto the sheetpoint???

 

Not realy?! Smiley Surprised

0 Likes
Message 8 of 20

Anonymous
Not applicable
Sub TryOne()
Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument
Dim dview As DrawingView
Set dview = oDoc.ActiveSheet.DrawingViews.Item(1)
Set oViewPos = ThisApplication.TransientGeometry.CreatePoint2d(dviewX, dviewY + 20)
dview.Position = oViewPos
End Sub

Thats what I have got so far.
What I now need is to change "+20" to "+ HalfSizeOfViewSizeY" and same for X-Achs.

0 Likes
Message 9 of 20

HermJan.Otterman
Advisor
Advisor

your getting there, I will help you.

 

this way you learn programming.

 

I use VB.net. with VBA you have to put some "set" and "call" statements.

 

a view has properties like: width and height, and there types are Double so:

- Dim halfWidth as Double = dView.Width/2

- Dim halfHeight as Double = dView.Height/2

 

you also have :

Dim viewScale as Double = dView.Scale

 

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


Message 10 of 20

HermJan.Otterman
Advisor
Advisor

to get to the sheets:

 

Dim oSheets as sheets

set oSheets =Thisapplication.Activedocument.Sheets

 

this oSheets is a collection of all sheets. the first sheet is:

 

dim oFirstSheet as sheet

Set oFirstSheet = oSheets.item(1)

 

the .iam is linked to a view. so if you have one view then you know that it is item 1 of the views collection. if you have more views, then you will have to find the right view, by name.

 

dim oDrwView as DrawingView

Set oDrwView = oFirstSheet.drawingviews("NameOfTheView")

 

then you need the related IAM

dim oIAM as Inventor.Document

Set oIAM = oDrwView.ReferencedDocumentDescriptor.ReferencedDocument

 

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


Message 11 of 20

Anonymous
Not applicable

Heart THX

 

 

Sub TryOne()
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
Dim oView As DrawingView
Set oView = oDrawDoc.ActiveSheet.DrawingViews.Item(1)

Dim halfWidth As Double
halfWidth = oView.Width / 2

Dim halfHeight As Double
halfHeight = oView.Height / 2

Set oViewPos = ThisApplication.TransientGeometry.CreatePoint2d(oViewX + halfWidth, oViewY + halfHeight)
oView.Position = oViewPos
End Sub

 

Now last to do is to get the IAM from FirstSheet and insert it into the new sheet. Smiley Happy

I think best is do go to the active sheet as FirstSheet. Read the view(1) value and write it into array and than create the new sheet and insert the array as a new view.

0 Likes
Message 12 of 20

Anonymous
Not applicable

Thank you very much. So far what I got.

 

IAM source from first sheet selected ... I think.

New Sheet is createt.

Sheet resized.

 

Now I have to insert a new view with the source of IAM. Correct?

 

Some code is bad... but works so far. Smiley LOL

 

Sub Test()

'Document as drawing and set active
Dim oDrawingDoc As DrawingDocument
Set oDrawingDoc = ThisApplication.ActiveDocument

'Sheets of active drawing
Dim oSheets As Sheets
Set oSheets = oDrawingDoc.Sheets

'Select first sheet of all
Dim oFirstSheet As Sheet
Set oFirstSheet = oSheets.Item(1)

'select first view in first sheet
Dim oView As DrawingView
Set oView = oFirstSheet.DrawingViews.Item(1)

'select IAM referenc from first view
Dim oIAM As Inventor.Document
Set oIAM = oView.ReferencedDocumentDescriptor.ReferencedDocument

'New Sheet
Dim oDrawingDocument1 As DrawingDocument
Dim oDrawingDocument2 As DrawingDocument
Dim oActSheets As Sheet
Set oActSheets = oDrawingDoc.ActiveSheet
        
Set oDrawingDocument1 = ThisApplication.ActiveDocument
Set oDrawingDocument2 = ThisApplication.Documents.Add(kDrawingDocumentObject, , False)
Set oSheet = oDrawingDocument2.Sheets.Item(oDrawingDocument2.Sheets.count)
oSheet.CopyTo oDrawingDocument1
oDrawingDocument2.Close True


For Each oActSheets In ThisApplication.ActiveDocument.Sheets
oActSheets.Activate
oSheetName = Left(oActSheets.Name, 5)
     
If oSheetName = "Blatt" Then
    
oDrawingDoc.ActiveSheet.Name = "Machine"
    
oActSheets.Size = DrawingSheetSizeEnum.kCustomDrawingSheetSize
oActSheets.Width = 250
oActSheets.Height = 160
    
MsgBox (oIAM)
    
End If
Next

End Sub
0 Likes
Message 13 of 20

HermJan.Otterman
Advisor
Advisor

nice !!

 

go to the Inventor help, click Programming/Api help

 

APIhelp.png

Type "View"

 

go to the 4th line, Working with drawing views

 

(you see all VBA code!)

 

Dim oDrawingDoc As DrawingDocument
Set oDrawingDoc = ThisApplication.ActiveDocument 
      
Dim oSheet As Sheet
Set oSheet = oDrawingDoc.Sheets.Item(1)
       
Dim oPoint1 As Point2d
Set oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(5#, 5#)
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.Documents.Open("c:\testpart.ipt", False)

Dim oView1 As DrawingView
Set oView1 = oSheet.DrawingViews.AddBaseView(oPartDoc, _
    oPoint1, 1#, kBottomViewOrientation, kHiddenLineDrawingViewStyle)

Call oPartDoc.Close(True)

 

 

 

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


Message 14 of 20

Anonymous
Not applicable

Thank you so much!

It works. Smiley Very Happy

 

Only one last thing. It adds the view as base (Mainview). But I have a special custom view for the iam.
How to change it? Cant finde any information for it and I dont knew how it is called in inventor. Smiley Surprised

Sub Test()

'Document as drawing and set active
Dim oDrawingDoc As DrawingDocument
Set oDrawingDoc = ThisApplication.ActiveDocument

'Sheets of active drawing
Dim oSheets As Sheets
Set oSheets = oDrawingDoc.Sheets

'Select first sheet of all
Dim oFirstSheet As Sheet
Set oFirstSheet = oSheets.Item(1)

'select first view in first sheet
Dim oView As DrawingView
Set oView = oFirstSheet.DrawingViews.Item(1)

'select IAM referenc from first view
Dim oIAM As Inventor.Document
Set oIAM = oView.ReferencedDocumentDescriptor.ReferencedDocument

'New Sheet
Dim oDrawingDocument1 As DrawingDocument
Dim oDrawingDocument2 As DrawingDocument
Dim oActSheets As Sheet
Set oActSheets = oDrawingDoc.ActiveSheet
        
Set oDrawingDocument1 = ThisApplication.ActiveDocument
Set oDrawingDocument2 = ThisApplication.Documents.Add(kDrawingDocumentObject, , False)
Set oSheet = oDrawingDocument2.Sheets.Item(oDrawingDocument2.Sheets.count)
oSheet.CopyTo oDrawingDocument1
oDrawingDocument2.Close True


For Each oActSheets In ThisApplication.ActiveDocument.Sheets
oActSheets.Activate
oSheetName = Left(oActSheets.Name, 5)
     
If oSheetName = "Blatt" Then
    
oDrawingDoc.ActiveSheet.Name = "Machine"
    
oActSheets.Size = DrawingSheetSizeEnum.kCustomDrawingSheetSize
oActSheets.Width = 250
oActSheets.Height = 160
    
MsgBox (oIAM)

Set oSheet = oDrawingDoc.ActiveSheet

Dim oPoint1 As Point2d
Set oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(0#, 0#)

Dim oView1 As DrawingView
Set oView1 = oSheet.DrawingViews.AddBaseView(oIAM, oPoint1, 1#, kFrontViewOrientation, kHiddenLineDrawingViewStyle)
    
Dim halfWidth As Double
halfWidth = oView1.Width / 2
Dim halfHeight As Double
halfHeight = oView1.Height / 2

Set oView1Pos = ThisApplication.TransientGeometry.CreatePoint2d(oView1X + halfWidth, oView1Y + halfHeight)
oView1.Position = oView1Pos
    
End If
Next

End Sub
0 Likes
Message 15 of 20

HermJan.Otterman
Advisor
Advisor

great that it works,

 

for the different views look in the API help for "DrawingViews object"

there you see the methods you can use like "addBaseView" or "AddProjectedView"

 

if you have you own special view, I thik you have to create a design view, in the assembly under representations.

 

then in code:

DrawingView.SetDesignViewRepresentation( Representation As String, [Associative] As Boolean )

 

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


Message 16 of 20

Anonymous
Not applicable

Ok. I dont understand that code. Smiley Sad

In assembly is a representation view called "DXF". And the code have to change the Base (Main) to DXF.

darstellung.png

 

 

Sub Ansicht()
Dim oDrawingDoc As DrawingDocument
Set oDrawingDoc = ThisApplication.ActiveDocument
Dim oActSheets As Sheet
Set oActSheets = oDrawingDoc.ActiveSheet
Dim oViewRepr As DrawingView
Set oViewRepr = oActSheets.DrawingViews.Item(1)

Dim ReprView As String
ReprView = "DXF"

Dim oView1 As DrawingView
Set oView1 = oViewRepr.DrawingView.SetDesignViewRepresentation(ReprView, False)
End Sub
0 Likes
Message 17 of 20

HermJan.Otterman
Advisor
Advisor
Accepted solution

can you try:

 

Set  oViewRepr.DrawingView.SetDesignViewRepresentation(ReprView, False)

 

or

call oViewRepr.DrawingView.SetDesignViewRepresentation(ReprView, False)

 

 

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


Message 18 of 20

Anonymous
Not applicable

Call does work! Smiley Happy

 

But I dont knew the command "call".

What is it?

 

Thank you. Now the complete code does work. Smiley Very Happy Smiley Very Happy Smiley Very Happy

You are the man!!! I would spent a beer. Smiley Happy

Message 19 of 20

HermJan.Otterman
Advisor
Advisor

great that you got it to work!!

 

in VB.net and iLogic you don't need to use call.

 

I think you can see it as "execute" or "do" something.

 

 

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


0 Likes
Message 20 of 20

Luisfmts
Enthusiast
Enthusiast

You're going straight to computer's heaven! Cause you just saved one of them from get smashed here.

 

Cheers


@HermJan.Otterman wrote:

can you try:

 

Set  oViewRepr.DrawingView.SetDesignViewRepresentation(ReprView, False)

 

or

call oViewRepr.DrawingView.SetDesignViewRepresentation(ReprView, False)

 

 


Smiley Very Happy