For problems like this, it is wise to consider the nature of the task. Are there already drawings that need this macro to be run on them or is this intended for future drawings?
You could write something that is very general:
- traverse all sketch entities on the sheet
- test if each is a line that bisects the drawing
- get its x coordinates
- traverse all views and select any that are located to the right of that coordinate
Potential problems with this approach involve determining which line should be used as the "boundary". The drafter might have inserted that construction line on any number of views/sketches, they might not have used a consistent line font, they might not have bisected the drawing completely. Checking whether or not a line bisects the drawing could be a matter of comparing the start and end points' y coordinates and testing whether or not they are above and below a threshold value (as a percentage of the overall height of the sheet). This way, you could easily tune the behavior by modifying those thresholds.
Alternatively, if the view that you want to select has a particular name and you create a drwdot file (drawing start model) with that view on it with the appropriate name assigned, it makes the macro easier to implement but less flexible. The macro would only have to traverse the views on a sheet and check whether or not they have a particular name.
Easier suggestion: check the x coordinate of all views, select the one that is right-most. It looks like the laser view is always to the right of the others, yeah?
Just a rough outline:
Dim vView as variant
Dim swSheet as sldworks.sheet
Dim swView as sldworks.view
Dim ViewToSelect as Sldworks.View
Dim xCoordinate as Double
Dim xMax as Double
'set sheet to current active sheet somehow
xMax = 0 'a more sophisticated thing to do would be to initialize this with the xCoordinate of the first view on the sheet (but 0 should suffice)
for each vView in swSheet.getViews
set swView = vView
xCoordinate = getXCoordinate(swView) 'getXCoordinate is an exercise left to the reader
if xMax < xCoordinate Then
xMax = xCoordinate
ViewToSelect = swView
Yes the laser view is always to the right of the others!!
This should work! I'll give it a shot!
Thanks a lot for the help John!!
That constructed line is in the template of our drawing and isn't set by a drafter each time.
It's set in the background... which I just realized could be another problem.
The macro will run on new drawings only since I want to verify different options
before saving as DWG... Old drawing should be already fixed if they had a problem.
I'll play around with the steps you mentioned to see if I can get anything started.
Just not sure about selecting views that are located right of a coordinate since I've
never played around with the SelectionManager.
I'll post back "when" I run into problems!!
Thanks for the help!
Cool, if you're struggling with getting the reference to the sheet or views or with getting the x coordinate from those objects, I could post something more concrete later today.
Thanks again John!
Here is what I came up with quickly...
Note: I changed ViewToSelect to String instead of Sldworks.View... Since the line "ViewToSelect = swView" didn't work.
As long as I get the name of the view I can later select that view!
Option Explicit Sub main() Dim vView As Variant Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim swDraw As SldWorks.DrawingDoc Dim swSheet As SldWorks.Sheet Dim swView As SldWorks.View Dim ViewToSelect As String Dim xCoordinate() As Double Dim xMax As Double Set swApp = CreateObject("SldWorks.Application") Set swModel = swApp.ActiveDoc Set swDraw = swModel Set swSheet = swDraw.GetCurrentSheet xMax = 0 For Each vView In swSheet.GetViews Set swView = vView xCoordinate = swView.Position If xMax < xCoordinate(0) Then xMax = xCoordinate(0) ViewToSelect = swView.Name End If Next vView Debug.Print "View = " + ViewToSelect Debug.Print " X = (" & xMax * 1000# & ") mm" End Sub
Thanks again for the help!
It should work as a View if you use "Set ViewToSelect = swView". That way you have a reference to the desired View object at the end of the loop and you don't have to go looking for it using the name.
You are right! It works with "Set ViewToSelect = swView".
Thanks for the tip!