I would like help in developing a macro to evaluate the views on a sheet, populate the sheet scale with the most common view scale, then change those view scales to match the sheet scales.
The values shall not be strings that you put into the method SetScale. They should be of data type Double.
See 2014 SolidWorks API Help - SetScale Method (ISheet) for details.
Set is used to assign a reference to object(s) in VBA and not to assign values/data to variables. This is one of the differences between VBA and VB.net.
Variables usually assigned just with = or directly returned in a method.
You could also skip the step with the TopSC and BottomSC if you like. The code would look like this.
Dim boolstatus As BooleanDim currentSheet As SheetDim Part As DrawingDoc boolstatus = currentSheet.SetScale(lViewScale(1, iPos), lViewScale(2, iPos), True, False)
Dim boolstatus As Boolean
Dim currentSheet As Sheet
Dim Part As DrawingDoc
boolstatus = currentSheet.SetScale(lViewScale(1, iPos), lViewScale(2, iPos), True, False)
But if you like to keep the TopSC and BottomSC it should look like this.
Dim boolstatus As BooleanDim currentSheet As SheetDim Part As DrawingDocDim TopSC As DoubleDim BottomSC As Double TopSC = lViewScale(1, iPos)BottomSC = lViewScale(2, iPos)boolstatus = currentSheet.SetScale(TopSC, BottomSC, True, False)
Dim TopSC As Double
Dim BottomSC As Double
TopSC = lViewScale(1, iPos)
BottomSC = lViewScale(2, iPos)
boolstatus = currentSheet.SetScale(TopSC, BottomSC, True, False)
Note that you also do not have to get a new object currentSheet since the current sheet in the macro I posted is possible to access with the swSheet.
I would recommend that you add the code above to the section where you find...
Debug.Print "Most used scale: " & lViewScale(1, iPos) & ":" & lViewScale(2, iPos) & " [" & lViewScale(0, iPos) & " views]"
Then you wouldn't have to think of the Sheet and DrawingDoc objects since they are already assigned in the macro.
Hope that this explains what you see and that it brings you forward. Just let me know if you have any further questions.
What kind of "help" would you like? Do you know how to write any macros now?
I would approach it like this...
You will need to use the ISheet and IView interface. Put each found scale into array or a collection and use a dimension (in array) or property in the collection to count the number of views that have matching scales.
Then evaulte the most used scale and set the scale to the sheet(s).
And finaly loop thru all the views once again and if the scale matches the set sheet scale, set the view to have scale from sheet.
In the interfaces you find methods that will help you to get and set the data.
These methods together with a array or collection could get and set the scales as you like for the views and sheets.
See this example about how to loop thru all sheets and views. Do not mind the things going on with the notes.. in your case that would be the scale of the view.
Just let me know if you need any further guidance.
I do know how to write and modify basic macros I have just not done anything with arrays to get all of the views on a given sheet, then do comparisons on which of those views has the highest common view scale. I think if I got to that point, I would be able to push that to sheet scale.
Examples work better for me than looking at the documentation... the example that was posted does help but I do not know how to do the count/comparison for the view scales.
Is this really that good of a macro to write? What if you have one main 1:1 drawing view on a sheet, and then two detail views that are 2:1. Do you really want the sheet scale to be changed to 2:1? How would you then designate the main view scale as 1:1 on the drawing sheet?
I've looked at what we are trying to accomplish and this is what our needs are. Every single drawing has at least 4 main views and none have more than 2 detail views. I realize at your company things may not be this way, but for what my company needs this is the path we are going down. This is a manual process we are trying to do right now and this would be a time saver.
I modified an previous macro I had to do this... one thing that I do know is not working properly is that you will get wrong values if you have multiple sheets and the second or next coming sheet is empty of views. Then the macro will find all views in the drawing (they will have hidden status).
This is a behaviour that I have seen before. In this case the API does not only get the visible views... and for some reason views from other sheets gets into the array of views for the sheet. This is sorted out by checking if the view is visible or not on the current sheet. I guess that this is due to my lack of knowledge to handle the API correctly (or an fault in the API, have not looked deeper into that yet).
Ah, almost forgot... what is the macro doing? Well, it will print out in the immediate window the number of views with scales and also the most used scale for each sheet.
Note that the loops in the macro use other methods than the previous example that I referenced earlier.
There might be easier ways but the attached example is how I would have done it with the knowledge I have. I tried to comment it as good I could. Have a look at it and let me know if you have any questions. Any ideas or comments from anyone is most welcome to improve the code.
Thank you Daniel! This is much more than I would have hoped someone to help me with!
I've gone forward and tried to set the sheet scale with the following:
Dim TopSC As String
Dim BottomSC As String
Set TopSC = lViewScale(1, iPos)
Set BottomSC = lViewScale(2, iPos)
It is throwing up a flag that says object required when I'm setting TopSC so I'm a little stuck. Any ideas?
A vast majority of the drawings we are doing this to only have a single sheet... I'll poke around a bit and see what it does for a second sheet, but I think this should work for me if I can get the sheet scale updated.
This works great for what I am driving for. Thank you also for your descriptions of what was going on. Trying really hard to wrap my head around this stuff and while much of the online documentation is helpful, it is nice to hear it in a more conversational format at times!
While running this macro I've run into the following issue. When the view scales and sheet scales differ, and their are annotations attached to the views the annotations move. My best guess is that the following is coming up as a default. Is there anyway to control this checkbox via API and uncheck it?
Yes you can. Set True to false in this line
currentSheet.SetScale(TopSC, BottomSC, True, False)
so that it looks like
currentSheet.SetScale(TopSC, BottomSC, False, False)
Thank you Deepak. That is what I had guessed, but I wasn't sure. Some of the guys here have had their annotations move on them but they handle scaling a little different than I do. Thanks for the help!
Retrieving data ...