When, in the opinion of the court, on the trial of any cause, civil or criminal, it is proper, in order to reach the ends of justice, for the court and jury to have a view or inspection of the property which is the subject of litigation, or the place at which the offense is charged to have been committed, or the place or places at which any material fact occurred, or of any material object or thing in any way connected with the evidence in the case, the court may, at its discretion, enter an order providing for such view or inspection as is herein below directed. After such order is entered, the whole organized court, consisting of the judge, jury, clerk, sheriff, and the necessary number of deputy sheriffs, shall proceed, in a body, to such place or places, property, object or thing to be so viewed or inspected, which shall be pointed out and explained to the court and jury by the witnesses in the case, who may, at the discretion of the court, be questioned by the court and by the representative of each side at the time and place of such view or inspection, in reference to any material fact brought out by such view or inspection. The court on such occasion shall remain in session from the time it leaves the courtroom till it returns thereto, and while so in session outside the courtroom it shall have full power to compel the attendance of witnesses, to preserve order, to prevent disturbance and to punish for contempt such as it has when sitting in the courtroom. In criminal trials all such views or inspections must be had before the whole court and in the presence of the accused, and the production of all evidence from all witnesses or objects, animate or inanimate, must be in his presence.